001: package org.enhydra.jawe;
002:
003: import java.awt.Component;
004: import java.io.File;
005:
006: import javax.swing.JFileChooser;
007: import javax.swing.JOptionPane;
008:
009: import org.enhydra.jawe.base.controller.JaWEController;
010:
011: /**
012: * Class which purpose is to provide static methods which are
013: * used by classes that represents program apstraction of
014: * XML elements. These methods offers support for reading or
015: * writting an XML document and for generating the tooltips for
016: * for the classes that needs it.
017: */
018: public class JaWEXMLUtil {
019:
020: // filter for XML files
021: private static JaWEXMLUtil.XMLFilter allFilter = new JaWEXMLUtil.XMLFilter(
022: null);
023: private static JaWEXMLUtil.XMLFilter xmlFilter = new JaWEXMLUtil.XMLFilter(
024: "xml");
025: private static JaWEXMLUtil.XMLFilter xpdlFilter = new JaWEXMLUtil.XMLFilter(
026: "xpdl");
027: private static JaWEXMLUtil.XMLFilter jpgFilter = new JaWEXMLUtil.XMLFilter(
028: "jpg");
029: private static JaWEXMLUtil.XMLFilter svgFilter = new JaWEXMLUtil.XMLFilter(
030: "svg");
031: private static JaWEXMLUtil.XMLFilter txtFilter = new JaWEXMLUtil.XMLFilter(
032: "txt");
033:
034: private static javax.swing.filechooser.FileFilter[] lastChoosenFilter = new javax.swing.filechooser.FileFilter[5];
035: static {
036: lastChoosenFilter[0] = xpdlFilter;
037: lastChoosenFilter[1] = jpgFilter;
038: lastChoosenFilter[2] = svgFilter;
039: lastChoosenFilter[3] = txtFilter;
040: lastChoosenFilter[4] = allFilter;
041: }
042:
043: private static JFileChooser chooser;
044:
045: static {
046: String userDir = System.getProperty("user.dir");
047: chooser = new JFileChooser(userDir);
048: }
049:
050: public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
051: public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
052:
053: //************************* XMLFILTER CLASS ***********************************
054: /**
055: * Used to filter XML files during opening or saving.
056: */
057: private static final class XMLFilter extends
058: javax.swing.filechooser.FileFilter {
059: private String myExtension = null;
060:
061: XMLFilter(String extension) {
062: this .myExtension = extension;
063: }
064:
065: public String getExtension() {
066: return myExtension;
067: }
068:
069: public boolean accept(File f) {
070: if (f != null) {
071: if (f.isDirectory()) {
072: return true;
073: }
074: if (myExtension != null) {
075: String extension = null;
076: String fName = f.getName();
077: int i = fName.lastIndexOf('.');
078: if (i > 0 && i < fName.length() - 1) {
079: extension = fName.substring(i + 1)
080: .toLowerCase();
081: }
082:
083: if (extension != null
084: && extension.equalsIgnoreCase(myExtension)) {
085: return true;
086: }
087: } else {
088: return true;
089: }
090: }
091: return false;
092: }
093:
094: public String getDescription() {
095: JaWEController jc = JaWEManager.getInstance()
096: .getJaWEController();
097: if (myExtension != null) {
098: return jc.getSettings().getLanguageDependentString(
099: myExtension.toUpperCase() + "Description");
100: }
101: return jc.getSettings().getLanguageDependentString(
102: "ALLDescription");
103: }
104: }
105:
106: //********************** END OF XMLFILTER CLASS ******************************
107:
108: /* Show a dialog with the given error message. */
109: public static void message(String message, int type) {
110: JaWEController jc = JaWEManager.getInstance()
111: .getJaWEController();
112: JOptionPane.showMessageDialog(jc.getJaWEFrame(), message, jc
113: .getSettings()
114: .getLanguageDependentString("DialogTitle"), type);
115: }
116:
117: /* Open a dialog and return the filename. Returns null if canceled.
118: * @param parent The parent component of dialog.
119: * @param message The message to write in the title of dialog
120: * @param mode 0 - open dialog, 1 - save dialog
121: * @param filteringMode if 0 - displays .xml and .xpdl files, if 1 - displays
122: * .jpg files, 2 - displays SVG files, 3 - displays .xml and .xpdl files,
123: * otherwise displays all files
124: * @param initialName The initial name of the file to be saved or opened
125: * @return The filename of opened/saved file.
126: */
127: public static String dialog(Component parent, String message,
128: int mode, int filteringMode, String initialName) {
129: chooser.updateUI();
130: chooser.setDialogTitle(message);
131: int setFilterIndex = 0;
132: switch (filteringMode) {
133: case 0:
134: chooser.setAcceptAllFileFilterUsed(false);
135: chooser.setFileFilter(allFilter);
136: chooser.setFileFilter(xmlFilter);
137: chooser.setFileFilter(xpdlFilter);
138: chooser.removeChoosableFileFilter(jpgFilter);
139: chooser.removeChoosableFileFilter(svgFilter);
140: chooser.removeChoosableFileFilter(txtFilter);
141: setFilterIndex = 0;
142: break;
143: case 1:
144: chooser.setAcceptAllFileFilterUsed(false);
145: chooser.setFileFilter(allFilter);
146: chooser.setFileFilter(jpgFilter);
147: chooser.removeChoosableFileFilter(xmlFilter);
148: chooser.removeChoosableFileFilter(xpdlFilter);
149: chooser.removeChoosableFileFilter(svgFilter);
150: chooser.removeChoosableFileFilter(txtFilter);
151: setFilterIndex = 1;
152: break;
153: case 2:
154: chooser.setAcceptAllFileFilterUsed(false);
155: chooser.setFileFilter(allFilter);
156: chooser.setFileFilter(svgFilter);
157: chooser.removeChoosableFileFilter(xmlFilter);
158: chooser.removeChoosableFileFilter(xpdlFilter);
159: chooser.removeChoosableFileFilter(jpgFilter);
160: chooser.removeChoosableFileFilter(txtFilter);
161: setFilterIndex = 2;
162: break;
163: case 3:
164: chooser.setAcceptAllFileFilterUsed(false);
165: chooser.setFileFilter(allFilter);
166: chooser.setFileFilter(txtFilter);
167: chooser.removeChoosableFileFilter(xmlFilter);
168: chooser.removeChoosableFileFilter(xpdlFilter);
169: chooser.removeChoosableFileFilter(jpgFilter);
170: chooser.removeChoosableFileFilter(svgFilter);
171: setFilterIndex = 3;
172: break;
173: default:
174: chooser.setAcceptAllFileFilterUsed(false);
175: chooser.setFileFilter(allFilter);
176: chooser.removeChoosableFileFilter(xmlFilter);
177: chooser.removeChoosableFileFilter(xpdlFilter);
178: chooser.removeChoosableFileFilter(jpgFilter);
179: chooser.removeChoosableFileFilter(svgFilter);
180: chooser.removeChoosableFileFilter(txtFilter);
181: setFilterIndex = 4;
182: break;
183: }
184: try {
185: chooser.setFileFilter(lastChoosenFilter[setFilterIndex]);
186: } catch (Exception ex) {
187: }
188:
189: if (initialName != null && initialName.length() > 0) {
190: File f = new File(initialName);
191: chooser.setSelectedFile(f);
192: chooser.ensureFileIsVisible(f);
193: }
194:
195: int returnVal = -1;
196: String fileName = null;
197:
198: while (true) {
199: if (mode == 0) {
200: returnVal = chooser.showOpenDialog(parent);
201: } else {
202: returnVal = chooser.showSaveDialog(parent);
203: }
204:
205: if (returnVal == JFileChooser.APPROVE_OPTION) {
206: File f = chooser.getSelectedFile();
207: fileName = f.getAbsolutePath();
208: // SAVING:
209: // - if extension isn't specified, tries to save the file with default extension
210: // - it will not save file with extension if the file with extension already exists
211: String extension = ((JaWEXMLUtil.XMLFilter) chooser
212: .getFileFilter()).getExtension();
213: int dotIndex = f.getName().lastIndexOf(".");
214:
215: String oldFilename = fileName;
216: JaWEController jc = JaWEManager.getInstance()
217: .getJaWEController();
218: if (mode == 1) {
219: if ((filteringMode >= 0 && filteringMode <= 2)
220: && extension != null && dotIndex == -1) {
221: fileName += "." + extension;
222: }
223: // check if user have choosed an existing filename
224: if (new File(fileName).exists()) {
225: int r = JOptionPane
226: .showConfirmDialog(
227: jc.getJaWEFrame(),
228: jc
229: .getSettings()
230: .getLanguageDependentString(
231: "WarningFileAlreadyExistsDoYouWantToReplaceIt"),
232: jc
233: .getSettings()
234: .getLanguageDependentString(
235: "DialogTitle"),
236: JOptionPane.YES_NO_OPTION);
237: if (r == JOptionPane.NO_OPTION) {
238: fileName = oldFilename;
239: continue;
240: }
241: }
242:
243: // OPENING:
244: // - if extension isn't specified, and given file doesn't exist, tries
245: // to open the .xml file with the same name
246: } else {
247: if (mode == 0 && !f.exists()) {
248: if ((filteringMode >= 0 && filteringMode <= 2)
249: && dotIndex == -1) {
250: fileName += "." + extension;
251: if (!new File(fileName).exists()) {
252: fileName = null;
253: }
254: } else {
255: fileName = null;
256: }
257: }
258: if (fileName == null) {
259: JaWEXMLUtil
260: .message(
261: jc
262: .getSettings()
263: .getLanguageDependentString(
264: "WarningFileDoesNotExistPleaseSelectExistingFileToOpen"),
265: JOptionPane.WARNING_MESSAGE);
266: fileName = oldFilename;
267: continue;
268: }
269: }
270: }
271: break;
272: }
273:
274: try {
275: lastChoosenFilter[setFilterIndex] = chooser.getFileFilter();
276: } catch (Exception ex) {
277: }
278:
279: return fileName;
280: }
281:
282: }
|