~veger/ganttproject/manual-import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
GanttProject is an opensource project management tool. License: GPL3
Copyright (C) 2011 GanttProject Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package net.sourceforge.ganttproject.export;

import java.awt.Component;
import java.io.File;
import java.net.URL;
import java.text.MessageFormat;
import java.util.List;

import javax.swing.filechooser.FileFilter;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.service.prefs.Preferences;

import net.sourceforge.ganttproject.IGanttProject;
import net.sourceforge.ganttproject.document.Document;
import net.sourceforge.ganttproject.export.ExportFileWizardImpl.State;
import net.sourceforge.ganttproject.filter.ExtensionBasedFileFilter;
import net.sourceforge.ganttproject.gui.FileChooserPageBase;
import net.sourceforge.ganttproject.gui.options.model.GPOption;
import net.sourceforge.ganttproject.gui.options.model.GPOptionGroup;
import net.sourceforge.ganttproject.gui.projectwizard.WizardImpl;
import net.sourceforge.ganttproject.language.GanttLanguage;

class FileChooserPage extends FileChooserPageBase {

    private final State myState;

    private final IGanttProject myProject;

    private final GPOptionGroup myWebPublishingGroup;

    FileChooserPage(State state, IGanttProject project, WizardImpl wizardImpl, Preferences prefs) {
        super(wizardImpl, prefs, false);
        myState = state;
        myProject = project;
        myWebPublishingGroup = new GPOptionGroup("exporter.webPublishing", new GPOption[]{state.getPublishInWebOption()});
        myWebPublishingGroup.setTitled(false);
    }

    @Override
    protected String getFileChooserTitle() {
        return GanttLanguage.getInstance().getText("selectFileToExport");
    }

    @Override
    public String getTitle() {
        return GanttLanguage.getInstance().getText("selectFileToExport");
    }

    @Override
    protected void loadPreferences() {
        super.loadPreferences();
        if (getPreferences().get(PREF_SELECTED_FILE, null) == null) {
            getChooser().setFile(proposeOutputFile(myProject, myState.getExporter()));
        } else {
            String proposedExtension = myState.getExporter().proposeFileExtension();
            if(proposedExtension != null) {
                File selectedFile = getChooser().getFile();
                String fileName = selectedFile.getName();
                int lastDot = fileName.lastIndexOf('.');
                if (lastDot < 0) {
                    // No extension available, add one
                    fileName += ".";
                    lastDot = selectedFile.getName().length();
                }
                String extension = fileName.substring(lastDot + 1);
                if (!extension.equals(proposedExtension)) {
                    getChooser().setFile(new File(selectedFile.getParent(), fileName.substring(0, lastDot+1) + proposedExtension));
                }
            }
        }
    }

    @Override
    protected void onSelectedUrlChange(URL selectedUrl) {
        myState.setUrl(selectedUrl);
        super.onSelectedUrlChange(selectedUrl);
    }

    @Override
    protected IStatus onSelectedFileChange(File file) {
        if (file.exists() && !file.canWrite()) {
            return new Status(IStatus.ERROR, "foo", IStatus.ERROR, "Can't write to file", null);
        }
        if (!file.exists() && !file.getParentFile().canWrite()) {
            return new Status(IStatus.ERROR, "foo", IStatus.ERROR, "Can't write to directory", null);
        }
        IStatus result = new Status(IStatus.OK, "foo", IStatus.OK, "", null);
        String proposedExtension = myState.getExporter().proposeFileExtension();
        if(proposedExtension != null) {
            if (false == file.getName().toLowerCase().endsWith(proposedExtension)) {
                result = new Status(
                    IStatus.OK, "foo", IStatus.OK,
                    MessageFormat.format("Note that the extension is not {0}",
                                         new Object[] {proposedExtension}),
                    null);
            }
        }
        IStatus setStatus = setSelectedFile(file);
        return setStatus.isOK() ? result : setStatus;
    }

    @Override
    protected Component createSecondaryOptionsPanel() {
        Component customUI = myState.getExporter().getCustomOptionsUI();
        return customUI == null ? super.createSecondaryOptionsPanel() : customUI;
    }

    static File proposeOutputFile(IGanttProject project, Exporter exporter) {
        String proposedExtension = exporter.proposeFileExtension();
        if (proposedExtension == null) {
            return null;
        }

        File result = null;
        Document projectDocument = project.getDocument();
        if (projectDocument != null) {
            File localFile = new File(projectDocument.getFilePath());
            if (localFile.exists()) {
                String name = localFile.getAbsolutePath();
                int lastDot = name.lastIndexOf('.');
                name = name.substring(0, lastDot) + "." + proposedExtension;
                result = new File(name);
            } else {
                File directory = localFile.getParentFile();
                if (directory.exists()) {
                    result = new File(directory, project.getProjectName() + "."
                            + proposedExtension);
                }
            }
        }
        if (result == null) {
            File userHome = new File(System.getProperty("user.home"));
            result = new File(userHome, project.getProjectName() + "."
                    + proposedExtension);
        }
        return result;
    }

    @Override
    protected FileFilter createFileFilter() {
        return new ExtensionBasedFileFilter(
                myState.getExporter().getFileNamePattern(), myState.getExporter()
                        .getFileTypeDescription());
    }

    @Override
    protected GPOptionGroup[] getOptionGroups() {
        GPOptionGroup[] exporterOptions = null;
        if (myState.getExporter()!=null) {
            List<GPOptionGroup> options = myState.getExporter().getSecondaryOptions();
            exporterOptions = options == null ? null : options.toArray(new GPOptionGroup[0]);
        }
        if (exporterOptions==null) {
            return new GPOptionGroup[] {myWebPublishingGroup};
        }
        GPOptionGroup[] result = new GPOptionGroup[exporterOptions.length+1];
        result[0] = myWebPublishingGroup;
        System.arraycopy(exporterOptions, 0, result, 1, exporterOptions.length);
        return result;
    }
}