001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.style.sld.editor;
016:
017: import java.io.File;
018: import java.text.MessageFormat;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import net.refractions.udig.style.sld.internal.Messages;
023: import net.refractions.udig.ui.operations.IOp;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.jface.action.IStatusLineManager;
027: import org.eclipse.jface.dialogs.MessageDialog;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.widgets.Display;
030: import org.eclipse.swt.widgets.FileDialog;
031: import org.eclipse.ui.IEditorPart;
032: import org.eclipse.ui.IEditorSite;
033: import org.eclipse.ui.IWorkbench;
034: import org.eclipse.ui.IWorkbenchPage;
035: import org.eclipse.ui.IWorkbenchWindow;
036: import org.eclipse.ui.PlatformUI;
037:
038: /**
039: * Abstract class can be used to implement your own "import from" operations.
040: */
041: public class ImportFrom implements IOp {
042:
043: /**
044: * Let the extention point perform any additional checks
045: * before bothering the users with a prompt.
046: * <p>
047: * The extention point lets you specify the exact interface (or class)
048: * required. This method returns <code>true</code>, but could be used
049: * to perform a more indepth check of say a Layer's Schema to pervent
050: * the export of a FeatureType with multiple Geometry attributes
051: * being exported as a Shapefile.
052: * </p>
053: * @param target Target to be considered for export
054: * @return <code>true</code> if non <code>null</code>, subclass can override with additional tests
055: */
056: public boolean canImport(Object target) {
057: return target != null;
058: }
059:
060: public void importFrom(Object target, File file,
061: IProgressMonitor monitor) throws Exception {
062: target = importFrom(file, monitor);
063: }
064:
065: /**
066: * Subclass should override to actually do something.
067: *
068: * @param target
069: * @param file
070: * @param monitor
071: * @throws Exception
072: */
073: public Object importFrom(File file, IProgressMonitor monitor)
074: throws Exception {
075: return null;
076: }
077:
078: /**
079: * Prompt to use for title (example: "Import from")
080: *
081: * @param target
082: * @return Prompt (may be based on target), should be internationalized
083: */
084: public String prompt(Object target) {
085: return Messages.ImportFrom_title;
086: }
087:
088: /**
089: * Default name for provided target.
090: * <p>
091: * Should make use of provided target's title if available.
092: * This will be combined with the first filter extention
093: * to form a valid filename.
094: * </p>
095: * @param target
096: * @return Default filename based on target, default is "new"
097: */
098: public String defaultName(Object target) {
099: return Messages.ImportExport_new;
100: }
101:
102: /**
103: * Override as required (example "*.sld").
104: *
105: * @return filter, default "*.*"
106: */
107: public String[] getFilterExtentions() {
108: List<String> filters = new ArrayList<String>();
109: for (String extention : getExtentions()) {
110: filters.add("*." + extention); //$NON-NLS-1$
111: }
112: return filters.toArray(new String[filters.size()]);
113: }
114:
115: /**
116: * Called by getFilterExtentions (example "sld").
117: *
118: * @return filter, default "*"
119: */
120: public String[] getExtentions() {
121: return new String[] { "*" }; //$NON-NLS-1$
122: }
123:
124: /**
125: * Override as required (example "Style Layer Descriptor document").
126: * <p>
127: * Care should be taken to internationalization these.
128: * </p>
129: * @return Filter names, default "All Files"
130: */
131: public String[] getFilterNames() {
132: return new String[] { Messages.ImportExport_all_files };
133: }
134:
135: /**
136: * Responsible for asking the user for a filename and calleding exporTo.
137: * <p>
138: * The following methods are used to control the process:
139: * <ul>
140: * <li>getFilterExtentions()
141: * <li>getFilterNames()
142: * <li>getPrompt(
143: */
144: protected class PromptAndImport implements Runnable {
145: Display display;
146: Object target;
147:
148: // results of prompt
149: File file;
150:
151: /**
152: * @param display
153: * @param target
154: * @param monitor
155: */
156: public PromptAndImport(Display display, Object target,
157: IProgressMonitor monitor) {
158: this .display = display;
159: this .target = target;
160: }
161:
162: /** Run with Display.asyncExec */
163: public void run() {
164: file = promptFile(display, target);
165: }
166:
167: /** @return File to export to or <code>null</code> to cancel */
168: File getFile() {
169: return file;
170: }
171: }
172:
173: /**
174: * Asks the users for a filename to import ...
175: * <p>
176: * If the user asks for an existing filename they will be prompted to confirm that they do
177: * indeed wish to replace. If they press Yes the existing file will be removed so the
178: * opperation can repalce it, if they select false the they will be reprompted.
179: * </p>
180: * @param display Display used to prompt with
181: * @param target Target (may be used to figure out prompt)
182: * @return File or <code>null</code> to be used to export
183: */
184: public File promptFile(Display display, Object target) {
185: //String name = defaultName( target );
186: String prompt = prompt(target);
187: FileDialog fileDialog = new FileDialog(
188: display.getActiveShell(), SWT.OPEN);
189:
190: fileDialog.setFilterExtensions(getFilterExtentions());
191: fileDialog.setFilterNames(getFilterNames());
192: //fileDialog.setFileName( name+"."+getExtentions()[0] );
193: fileDialog.setText(prompt);
194:
195: String path = fileDialog.open();
196: if (path == null) {
197: return null; // user canceled
198: }
199: File file = new File(path);
200: if (!file.exists()) {
201: MessageDialog.openError(display.getActiveShell(), prompt,
202: file.getAbsolutePath() + " does not exist."); //$NON-NLS-1$
203: return null;
204: }
205: return file;
206: }
207:
208: /**
209: * This method is called in a non ui thread...
210: *
211: */
212: public void op(Display display, Object target,
213: IProgressMonitor monitor) throws Exception {
214: if (!canImport(target)) {
215: return; // should we log this? Or disable the op...
216: }
217: PromptAndImport prompt = new PromptAndImport(display, target,
218: monitor);
219: display.syncExec(prompt);
220: File file = prompt.getFile();
221:
222: status(MessageFormat.format(Messages.ImportFrom_reading, file));
223: importFrom(target, file, monitor);
224: status(MessageFormat.format(Messages.ImportFrom_finished, file));
225: }
226:
227: /**
228: *
229: * @param msg Display msg on status bar (if possible)
230: */
231: public void status(final String msg) {
232: final IStatusLineManager statusBar = getStatusBar();
233: final Display display = Display.getCurrent();
234:
235: if (statusBar == null || display == null)
236: return;
237:
238: display.syncExec(new Runnable() {
239: public void run() {
240: statusBar.setMessage(msg);
241: }
242: });
243: }
244:
245: private IStatusLineManager getStatusBar() {
246: IEditorSite site = getEditorSite();
247: if (site == null)
248: return null;
249: return site.getActionBars().getStatusLineManager();
250: }
251:
252: private IEditorSite getEditorSite() {
253: IWorkbenchWindow window = getWindow();
254: if (window == null)
255: return null;
256: IWorkbenchPage page = window.getActivePage();
257: if (page == null)
258: return null;
259: IEditorPart part = page.getActiveEditor();
260: if (part == null)
261: return null;
262: return (IEditorSite) part.getSite();
263: }
264:
265: private IWorkbenchWindow getWindow() {
266: IWorkbench bench = PlatformUI.getWorkbench();
267: if (bench == null)
268: return null;
269: IWorkbenchWindow window = bench.getActiveWorkbenchWindow();
270: if (window == null) {
271: if (bench.getWorkbenchWindowCount() > 0)
272: window = bench.getWorkbenchWindows()[0];
273: }
274: return window;
275: }
276:
277: public String[] addTo(String[] more, String[] original) {
278: int offset = more.length;
279: String[] newArray = new String[original.length + offset];
280: //add new values
281: for (int i = 0; i < offset; i++) {
282: newArray[i] = more[i];
283: }
284: //add old values
285: for (int i = 0; i < original.length; i++) {
286: newArray[i + offset] = original[i];
287: }
288: //return
289: return newArray;
290: }
291:
292: }
|