001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.session;
051:
052: import java.awt.Component;
053: import java.io.File;
054:
055: import javax.swing.Icon;
056: import javax.swing.JFileChooser;
057: import javax.swing.filechooser.FileFilter;
058: import javax.swing.filechooser.FileView;
059:
060: import com.projity.strings.Messages;
061: import com.projity.util.Environment;
062:
063: public class FileHelper {
064: public static final String DEFAULT_FILE_EXTENSION = "pod";
065: public static final int PROJITY_FILE_TYPE = 1;
066: public static final int MSP_FILE_TYPE = 101;
067: //public static final int SERVER_FILE_TYPE=1000;
068:
069: private JFileChooser fileChooser = null;
070:
071: private JFileChooser getFileChooser() {
072: if (fileChooser == null)
073: fileChooser = new JFileChooser();
074: return fileChooser;
075: }
076:
077: public String chooseFileName(final boolean save,
078: String selectedFileName, Component fileChooserParent) {
079: if (!Environment.getStandAlone()
080: && save
081: && selectedFileName != null
082: && selectedFileName.endsWith("."
083: + DEFAULT_FILE_EXTENSION)) {
084: selectedFileName = changeFileExtension(selectedFileName,
085: save ? "xml" : "mpp");
086: }
087: final JFileChooser fileChooser = getFileChooser();
088: fileChooser.setDialogType(save ? JFileChooser.SAVE_DIALOG
089: : JFileChooser.OPEN_DIALOG);
090: fileChooser.resetChoosableFileFilters();
091: if (selectedFileName == null) {
092: try {
093: fileChooser.setCurrentDirectory(new File(System
094: .getProperty("user.home")
095: + File.separator + "OpenProj"));
096: } catch (Exception e) {
097: }
098: } else
099: fileChooser.setSelectedFile(new File(selectedFileName));
100:
101: FileView fileView = new FileView() {
102: public Icon getIcon(File f) {
103: String extension = getFileExtension(f.getName());
104: if (extension != null) {
105: if ("pod".equals(extension)) {
106: return FileHelper.getIcon("format.projity");
107: }
108: //Icon icon=fileChooser.getFileSystemView().getSystemIcon(f);
109: // if ("mpp".equals(extension) || "mpx".equals(extension) || "planner".equals(extension)){
110: // return LocalSession.getIcon("format.other");
111: // }
112: }
113: return null;
114: }
115: };
116: fileChooser.setFileView(fileView);
117:
118: final FileFilter projityFilter = new FileFilter() {
119: public boolean accept(File f) {
120: return f.isDirectory()
121: || f.getName().toLowerCase().endsWith(
122: "." + DEFAULT_FILE_EXTENSION);
123: }
124:
125: public String getDescription() {
126: //return "Projity";
127: return Messages.getString("File.projity") + " (*."
128: + DEFAULT_FILE_EXTENSION + ")";
129: }
130: };
131: final FileFilter microsoftFilter = new FileFilter() {
132: public boolean accept(File f) {
133: boolean isAllowed;
134: String n = f.getName().toLowerCase();
135: if (save)
136: isAllowed = false;
137: else
138: isAllowed = n.endsWith(".mpp")
139: || n.endsWith(".mpx");
140: return f.isDirectory() || isAllowed;
141: }
142:
143: public String getDescription() {
144: return Messages.getString("File.microsoft")
145: + " (*.mpp, *.mpx)";
146: }
147:
148: };
149: final FileFilter microsoftXMLFilter = new FileFilter() {
150: public boolean accept(File f) {
151: boolean isAllowed;
152: String n = f.getName().toLowerCase();
153: if (save)
154: isAllowed = n.endsWith(".xml");
155: else
156: isAllowed = n.endsWith(".xml");
157: return f.isDirectory() || isAllowed;
158: }
159:
160: public String getDescription() {
161: return Messages.getString("File.microsoftXML")
162: + " (*.xml)";
163: }
164:
165: };
166: final FileFilter plannerFilter = new FileFilter() {
167: public boolean accept(File f) {
168: boolean isAllowed;
169: String n = f.getName().toLowerCase();
170: if (save)
171: isAllowed = false;
172: else
173: isAllowed = n.endsWith("*.planner");
174: return f.isDirectory() || isAllowed;
175: }
176:
177: public String getDescription() {
178: return Messages.getString("File.planner")
179: + " (*.planner)";
180: }
181:
182: };
183: FileFilter projectFilter = new FileFilter() {
184: public boolean accept(File f) {
185: if (/*Environment.getStandAlone()&&*/projityFilter
186: .accept(f))
187: return true;
188: if (microsoftXMLFilter.accept(f))
189: return true;
190: if (plannerFilter.accept(f))
191: return true;
192: if (microsoftFilter.accept(f))
193: return true;
194: return false;
195: }
196:
197: public String getDescription() {
198: return Messages.getString("File.projects");
199: }
200:
201: };
202:
203: if (save) {
204: if (microsoftFilter.accept(fileChooser.getSelectedFile())) { //To select the good filter by default
205: if (Environment.getStandAlone())
206: fileChooser.addChoosableFileFilter(projityFilter);
207: fileChooser.addChoosableFileFilter(microsoftXMLFilter);
208: } else {
209: fileChooser.addChoosableFileFilter(microsoftXMLFilter);
210: if (Environment.getStandAlone())
211: fileChooser.addChoosableFileFilter(projityFilter);
212: }
213: } else {
214: /*if (Environment.getStandAlone())*/fileChooser
215: .addChoosableFileFilter(projityFilter);
216: fileChooser.addChoosableFileFilter(microsoftFilter);
217: fileChooser.addChoosableFileFilter(microsoftXMLFilter);
218: fileChooser.addChoosableFileFilter(plannerFilter);
219: fileChooser.addChoosableFileFilter(projectFilter);
220: }
221:
222: if (fileChooser.showDialog(fileChooserParent, null) != JFileChooser.APPROVE_OPTION)
223: return null;
224: File file = fileChooser.getSelectedFile();
225: String fileName = file.toString();
226: FileFilter currentFilter = fileChooser.getFileFilter();
227: if (save) {
228: if (currentFilter == microsoftXMLFilter) {
229: if (!fileName.endsWith(".xml"))
230: fileName += ".xml";
231: } else if (!fileName.endsWith(".pod"))
232: fileName += ".pod";
233: }
234: return fileName;
235:
236: }
237:
238: public static boolean isFileNameAllowed(String fileName,
239: boolean save) {
240: String n = fileName.toLowerCase();
241: if (save)
242: return n.endsWith(".xml")
243: || n.endsWith("." + DEFAULT_FILE_EXTENSION);
244: else
245: return n.endsWith(".xml") || n.endsWith(".mpp")
246: || n.endsWith(".mpx") || n.endsWith(".planner")
247: || n.endsWith("." + DEFAULT_FILE_EXTENSION)
248: || n.endsWith(".mpx");
249: }
250:
251: public static String getFileExtension(String fileName) {
252: int i = fileName.lastIndexOf('.');
253: if (i > 0 && i < fileName.length() - 1)
254: return fileName.substring(i + 1).toLowerCase();
255: return null;
256: }
257:
258: public static String changeFileExtension(String fileName,
259: int fileType) {
260: return changeFileExtension(fileName, getFileExtension(fileType));
261: }
262:
263: public static String changeFileExtension(String fileName,
264: String extension) {
265: if (fileName == null)
266: return null;
267: int i = fileName.lastIndexOf('.');
268: if (i <= 0)
269: return fileName + "." + extension;
270: else
271: return fileName.substring(0, i) + "." + extension;
272: }
273:
274: public static Icon getIcon(String name) {
275: try {
276: return (Icon) Class.forName(
277: "com.projity.pm.graphic.IconManager").getMethod(
278: "getIcon", new Class[] { String.class }).invoke(
279: null, new Object[] { name });
280: } catch (Exception e) {
281: }
282: return null;
283:
284: }
285:
286: public static String getFileExtension(int fileType) {
287: switch (fileType) {
288: //case FileHelper.SERVER_FILE_TYPE: return null;
289: case FileHelper.PROJITY_FILE_TYPE:
290: return DEFAULT_FILE_EXTENSION;
291: case FileHelper.MSP_FILE_TYPE:
292: return "xml";
293: default:
294: return DEFAULT_FILE_EXTENSION;
295: }
296: }
297:
298: public static int getFileType(String fileName) {
299: if (fileName == null)
300: return 0;
301: fileName = fileName.toLowerCase();
302: if (fileName.endsWith(DEFAULT_FILE_EXTENSION))
303: return PROJITY_FILE_TYPE;
304: if (fileName.endsWith("mpp") || fileName.endsWith("mpx")
305: || fileName.endsWith("xml")
306: || fileName.endsWith("planner"))
307: return MSP_FILE_TYPE;
308: return 0;
309: }
310:
311: }
|