Skip to content

Commit b251394

Browse files
committed
Replaced file format options in MSProject, CSV, Image exporters with a single class.
Completed the exporter chooser page
1 parent c89c426 commit b251394

File tree

11 files changed

+149
-150
lines changed

11 files changed

+149
-150
lines changed

biz.ganttproject.core/src/main/java/biz/ganttproject/core/option/DefaultEnumerationOption.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public DefaultEnumerationOption(String id, T[] values) {
3232
reloadValues(Arrays.asList(values));
3333
}
3434

35+
public DefaultEnumerationOption(String id, List<T> values) {
36+
super(id);
37+
myValues = new ArrayList<>();
38+
reloadValues(values);
39+
}
40+
3541
protected void reloadValues(List<T> values) {
3642
List<String> oldValues = new ArrayList<>(myValues);
3743
myValues.clear();

biz.ganttproject.core/src/main/java/biz/ganttproject/core/option/ObservableProperty.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ class ObservableString(
101101
class ObservableBoolean(id: String, initValue: Boolean = false)
102102
: ObservableProperty<Boolean>(id,initValue)
103103

104-
class ObservableEnum<E : Enum<E>>(id: String, initValue: E, val allValues: Array<E>)
105-
: ObservableProperty<E>(id,initValue)
104+
class ObservableEnum<E : Enum<E>>(id: String, initValue: E, val allValues: List<E>)
105+
: ObservableProperty<E>(id,initValue) {
106+
constructor(id: String, initValue: E, allValues: Array<E>) : this(id, initValue, allValues.toList())
107+
}
106108

107109
class ObservableChoice<T>(id: String, initValue: T, val allValues: List<T>, val converter: StringConverter<T>)
108110
: ObservableProperty<T>(id, initValue)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2026 Dmitry Barashev, BarD Software s.r.o
3+
4+
This file is part of GanttProject, an opensource project management tool.
5+
6+
GanttProject is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
GanttProject is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with GanttProject. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package biz.ganttproject.impex.msproject2
20+
21+
enum class MsProjectFileFormat(val extension: String) {
22+
MPX("mpx"), MSPDI("xml")
23+
}

biz.ganttproject.impex.msproject2/src/main/java/biz/ganttproject/impex/msproject2/ExporterToMsProjectFile.java

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ of the License, or (at your option) any later version.
2727
import net.sf.mpxj.mpx.MPXWriter;
2828
import net.sf.mpxj.mspdi.MSPDIWriter;
2929
import net.sf.mpxj.writer.ProjectWriter;
30+
import net.sourceforge.ganttproject.export.ExportFileFormatOption;
3031
import net.sourceforge.ganttproject.export.ExporterBase;
3132
import org.eclipse.core.runtime.IStatus;
3233
import org.eclipse.core.runtime.Status;
@@ -43,22 +44,8 @@ of the License, or (at your option) any later version.
4344
*/
4445
public class ExporterToMsProjectFile extends ExporterBase {
4546

46-
private static final String[] FILE_FORMAT_IDS = new String[] { "impex.msproject.fileformat.mpx",
47-
"impex.msproject.fileformat.mspdi" };
48-
49-
private static final String[] FILE_EXTENSIONS = new String[] { "mpx", "xml" };
50-
51-
private String myFileFormat = FILE_FORMAT_IDS[0];
52-
53-
private final EnumerationOption myFileFormatOption = new DefaultEnumerationOption<Object>("impex.msproject.fileformat",
54-
FILE_FORMAT_IDS) {
55-
@Override
56-
public void commit() {
57-
super.commit();
58-
ExporterToMsProjectFile.this.myFileFormat = getValue();
59-
}
60-
};
61-
47+
private final ExportFileFormatOption<MsProjectFileFormat> myFileFormatOption = new ExportFileFormatOption(
48+
"impex.msproject.fileformat", MsProjectFileFormat.MPX, Arrays.stream(MsProjectFileFormat.values()).toList());
6249
private final LocaleOption myLanguageOption = new LocaleOption();
6350

6451
private final GPOptionGroup myOptions = new GPOptionGroup("exporter.msproject", new GPOption[] { myFileFormatOption });
@@ -68,9 +55,6 @@ public void commit() {
6855
public ExporterToMsProjectFile() {
6956
myOptions.setTitled(false);
7057
myMPXOptions.setTitled(false);
71-
myFileFormatOption.lock();
72-
myFileFormatOption.setValue(FILE_FORMAT_IDS[0]);
73-
myFileFormatOption.commit();
7458
myLanguageOption.setSelectedLocale(language.getLocale());
7559
}
7660

@@ -86,7 +70,7 @@ public GPOptionGroup getOptions() {
8670

8771
@Override
8872
public List<GPOptionGroup> getSecondaryOptions() {
89-
return FILE_FORMAT_IDS[0].equals(myFileFormat) ? Collections.singletonList(myMPXOptions)
73+
return myFileFormatOption.getSelectedValue() == MsProjectFileFormat.MPX ? Collections.singletonList(myMPXOptions)
9074
: Collections.<GPOptionGroup> emptyList();
9175
}
9276

@@ -97,18 +81,13 @@ public Component getCustomOptionsUI() {
9781

9882
@Override
9983
public String getFileNamePattern() {
100-
return myFileFormat;
84+
return myFileFormatOption.getSelectedValue().getExtension();
10185
}
10286

10387
@Override
10488
protected void setFormat(String format) {
105-
for (int i = 0; i < FILE_EXTENSIONS.length; i++) {
106-
if (FILE_EXTENSIONS[i].equalsIgnoreCase(format)) {
107-
myFileFormatOption.setValue(FILE_FORMAT_IDS[i]);
108-
myFileFormatOption.commit();
109-
break;
110-
}
111-
}
89+
MsProjectFileFormat.getEntries().stream().filter( f -> f.getExtension().equalsIgnoreCase(format))
90+
.findFirst().ifPresent(myFileFormatOption::setSelectedValue);
11291
}
11392

11493
@Override
@@ -140,14 +119,14 @@ protected IStatus run() {
140119
}
141120

142121
private ProjectWriter createProjectWriter() {
143-
if (FILE_FORMAT_IDS[0].equals(myFileFormat)) {
122+
if (myFileFormatOption.getSelectedValue() == MsProjectFileFormat.MPX) {
144123
MPXWriter result = new MPXWriter();
145124
if (myLanguageOption.getSelectedLocale() != null) {
146125
result.setLocale(myLanguageOption.getSelectedLocale());
147126
}
148127
return result;
149128
}
150-
if (FILE_FORMAT_IDS[1].equals(myFileFormat)) {
129+
if (myFileFormatOption.getSelectedValue() == MsProjectFileFormat.MSPDI) {
151130
return new MSPDIWriter();
152131
}
153132
assert false : "Should not be here";
@@ -160,17 +139,11 @@ public String proposeFileExtension() {
160139
}
161140

162141
private String getSelectedFormatExtension() {
163-
for (int i = 0; i < FILE_FORMAT_IDS.length; i++) {
164-
if (myFileFormat.equals(FILE_FORMAT_IDS[i])) {
165-
return FILE_EXTENSIONS[i];
166-
}
167-
}
168-
throw new IllegalStateException("Selected format=" + myFileFormat + " has not been found in known formats:"
169-
+ Arrays.asList(FILE_FORMAT_IDS));
142+
return myFileFormatOption.getSelectedValue().getExtension();
170143
}
171144

172145
@Override
173146
public String[] getFileExtensions() {
174-
return FILE_EXTENSIONS;
147+
return MsProjectFileFormat.getEntries().stream().map(MsProjectFileFormat::getExtension).toArray(String[]::new);
175148
}
176149
}

ganttproject/src/main/java/biz/ganttproject/app/PropertySheet.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ class PropertyPaneBuilderImpl(private val localizer: Localizer, private val grid
472472
val options: PropertyDisplayOptions<*>?
473473
): RowBuilder {
474474
private fun createLabel(item: OptionRowBuilder): Label {
475-
return Label(item.label)
475+
return Label(item.label).also {
476+
it.styleClass.add("label")
477+
}
476478
}
477479

478480
override fun build(grid: GridPane, idx: Int): Int {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2026 Dmitry Barashev, BarD Software s.r.o
3+
4+
This file is part of GanttProject, an opensource project management tool.
5+
6+
GanttProject is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
GanttProject is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with GanttProject. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package net.sourceforge.ganttproject.export
20+
21+
import biz.ganttproject.core.option.DefaultEnumerationOption
22+
import biz.ganttproject.core.option.ObservableEnum
23+
import biz.ganttproject.core.option.PropertyPaneBuilder
24+
25+
class ExportFileFormatOption<E: Enum<E>>(id: String, initialValue: E, allValues: List<E>): DefaultEnumerationOption<E>(id, allValues) {
26+
private val observable = ObservableEnum(id, initialValue, allValues)
27+
init {
28+
selectedValue = initialValue
29+
observable.addWatcher{ evt ->
30+
setSelectedValue(evt.newValue)
31+
}
32+
}
33+
34+
override fun visitPropertyPaneBuilder(builder: PropertyPaneBuilder) {
35+
builder.dropdown(observable, null)
36+
}
37+
}
38+
39+
enum class ImageFileFormat(val extension: String) {
40+
PNG("png"), JPEG("jpg")
41+
}
42+
43+
enum class CsvFileFormat(val extension: String) {
44+
CSV("csv"), EXCEL("xls")
45+
}

ganttproject/src/main/java/net/sourceforge/ganttproject/export/ExporterChooserPageFx.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package net.sourceforge.ganttproject.export
23

34
import biz.ganttproject.app.*
@@ -49,9 +50,13 @@ private val propertyLocalizer = i18n {
4950
fallback {
5051
default()
5152
transform {
52-
val replaced = if (it.contains("format.value")) it.replace("format.value", "fileformat") else it
53+
val replaced =
54+
if (it.contains(".format.value")) it.replace(".format.value", ".fileformat")
55+
else if (it.contains("fileformat.value")) it.replace(".value", "")
56+
else it
5357
"optionValue.$replaced.label"
5458
}
59+
//debug("exporter.dropdown.value")
5560
}
5661
}
5762
}

ganttproject/src/main/java/net/sourceforge/ganttproject/export/ExporterToCSV.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ of the License, or (at your option) any later version.
2222
import biz.ganttproject.impex.csv.GanttCSVExport;
2323
import biz.ganttproject.impex.csv.SpreadsheetFormat;
2424
import biz.ganttproject.impex.csv.SpreadsheetWriter;
25-
import kotlin.Unit;
2625
import net.sourceforge.ganttproject.GPLogger;
2726
import net.sourceforge.ganttproject.GanttProject;
2827
import net.sourceforge.ganttproject.io.CSVOptions;
@@ -36,28 +35,12 @@ of the License, or (at your option) any later version.
3635
import java.io.FileOutputStream;
3736
import java.io.IOException;
3837
import java.io.OutputStream;
38+
import java.util.Arrays;
3939
import java.util.List;
4040
import java.util.stream.Stream;
4141

4242
public class ExporterToCSV extends ExporterBase {
43-
static class FormatOption extends DefaultEnumerationOption<SpreadsheetFormat> {
44-
private ObservableEnum<SpreadsheetFormat> observable = new ObservableEnum<>("impex.csv.format", SpreadsheetFormat.CSV, SpreadsheetFormat.values());
45-
FormatOption() {
46-
super("impex.csv.format", SpreadsheetFormat.values());
47-
setSelectedValue(SpreadsheetFormat.CSV);
48-
observable.addWatcher(evt -> {
49-
setSelectedValue(evt.getNewValue());
50-
return Unit.INSTANCE;
51-
});
52-
}
53-
54-
@Override
55-
public void visitPropertyPaneBuilder(PropertyPaneBuilder builder) {
56-
builder.dropdown(observable, null);
57-
}
58-
}
59-
60-
private final FormatOption myFormatOption = new FormatOption();
43+
private final ExportFileFormatOption<SpreadsheetFormat> myFormatOption = new ExportFileFormatOption<>("impex.csv.format", SpreadsheetFormat.CSV, Arrays.stream(SpreadsheetFormat.values()).toList());
6144

6245

6346
private final GPOptionGroup myOptions = new GPOptionGroup("impex.csv", new GPOption[]{myFormatOption});

0 commit comments

Comments
 (0)