001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.printing.ui.actions;
010:
011: import java.awt.print.PageFormat;
012: import java.awt.print.PrinterJob;
013: import java.text.MessageFormat;
014: import java.util.Iterator;
015: import java.util.Locale;
016:
017: import net.refractions.udig.printing.model.Box;
018: import net.refractions.udig.printing.model.ModelFactory;
019: import net.refractions.udig.printing.model.Page;
020: import net.refractions.udig.printing.ui.Template;
021: import net.refractions.udig.printing.ui.TemplateFactory;
022: import net.refractions.udig.printing.ui.internal.BasicTemplateFactory;
023: import net.refractions.udig.printing.ui.internal.Messages;
024: import net.refractions.udig.printing.ui.internal.PrintingPlugin;
025: import net.refractions.udig.project.internal.Map;
026: import net.refractions.udig.project.internal.Project;
027: import net.refractions.udig.project.ui.internal.MapEditorInput;
028: import net.refractions.udig.project.ui.internal.ProjectExplorer;
029:
030: import org.eclipse.draw2d.geometry.Dimension;
031: import org.eclipse.emf.ecore.util.EcoreUtil;
032: import org.eclipse.jface.action.IAction;
033: import org.eclipse.jface.dialogs.MessageDialog;
034: import org.eclipse.jface.viewers.ArrayContentProvider;
035: import org.eclipse.jface.viewers.ILabelProvider;
036: import org.eclipse.jface.viewers.ISelection;
037: import org.eclipse.jface.viewers.LabelProvider;
038: import org.eclipse.jface.viewers.StructuredSelection;
039: import org.eclipse.jface.window.Window;
040: import org.eclipse.swt.widgets.Display;
041: import org.eclipse.ui.IEditorActionDelegate;
042: import org.eclipse.ui.IEditorInput;
043: import org.eclipse.ui.IEditorPart;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.dialogs.ListDialog;
046:
047: /**
048: * Creates a Page using the current map
049: *
050: * @author Richard Gould
051: */
052: public class CreatePageAction implements IEditorActionDelegate {
053:
054: public void run(IAction action) {
055: IEditorInput input = PlatformUI.getWorkbench()
056: .getActiveWorkbenchWindow().getActivePage()
057: .getActiveEditor().getEditorInput();
058:
059: if (!(input instanceof MapEditorInput)) {
060: MessageDialog.openError(Display.getDefault()
061: .getActiveShell(),
062: Messages.CreatePageAction_printError_title,
063: Messages.CreatePageAction_printError_text);
064: }
065:
066: final java.util.Map<String, TemplateFactory> templateFactories = PrintingPlugin
067: .getDefault().getTemplateFactories();
068:
069: //TODO move to a preference initializer
070: PrintingPlugin
071: .getDefault()
072: .getPluginPreferences()
073: .setValue(PrintingPlugin.PREF_DEFAULT_TEMPLATE,
074: "net.refractions.udig.printing.ui.internal.BasicTemplate"); //$NON-NLS-1$
075:
076: String defaultTemplate = PrintingPlugin.getDefault()
077: .getPluginPreferences().getString(
078: PrintingPlugin.PREF_DEFAULT_TEMPLATE);
079:
080: TemplateFactory templateFactory;
081:
082: ListDialog dialog = new ListDialog(Display.getDefault()
083: .getActiveShell());
084: dialog.setTitle(Messages.CreatePageAction_dialog_title);
085: dialog.setMessage(Messages.CreatePageAction_dialog_message);
086: dialog.setInput(templateFactories.values());
087: ArrayContentProvider provider = new ArrayContentProvider();
088: dialog.setContentProvider(provider);
089:
090: ILabelProvider labelProvider = new LabelProvider() {
091: public String getText(Object element) {
092: return ((TemplateFactory) element).getName();
093: }
094: };
095: dialog.setLabelProvider(labelProvider);
096:
097: templateFactory = (TemplateFactory) PrintingPlugin.getDefault()
098: .getTemplateFactories().get(defaultTemplate);
099:
100: dialog.setInitialSelections(new Object[] { templateFactory });
101: int result = dialog.open();
102: if (result == Window.CANCEL || dialog.getResult().length == 0) {
103: return;
104: }
105:
106: Template template = null;
107:
108: templateFactory = ((TemplateFactory) dialog.getResult()[0]);
109:
110: if (templateFactory == null) {
111: PrintingPlugin
112: .log(
113: Messages.CreatePageAction_error_cannotFindDefaultTemplate,
114: null);
115:
116: TemplateFactory firstAvailable = (TemplateFactory) templateFactories
117: .values().iterator().next();
118: if (firstAvailable == null) {
119: PrintingPlugin
120: .log(
121: "Unable to locate any templates, resorting to hard coded default.",
122: null);
123: template = new BasicTemplateFactory().createTemplate();
124: } else {
125: template = firstAvailable.createTemplate();
126: }
127: } else {
128: template = templateFactory.createTemplate();
129: }
130:
131: Map map = null;
132: Project project = null;
133:
134: Map oldMap = (Map) ((MapEditorInput) input).getProjectElement();
135: project = oldMap.getProjectInternal();
136:
137: map = (Map) EcoreUtil.copy(oldMap);
138:
139: project.getElementsInternal().add(map);
140:
141: MessageFormat formatter = new MessageFormat(
142: Messages.CreatePageAction_newPageName, Locale
143: .getDefault());
144: map.setName(formatter.format(new Object[] { map.getName() }));
145:
146: PageFormat pageFormat = PrinterJob.getPrinterJob()
147: .defaultPage();
148: int width = new Double(pageFormat.getImageableWidth())
149: .intValue();
150: int height = new Double(pageFormat.getImageableHeight())
151: .intValue();
152:
153: Page page = ModelFactory.eINSTANCE.createPage();
154:
155: page.setSize(new Dimension(width, height));
156: if (page.getName() == null || page.getName().length() == 0) {
157: page.setName("Untitled");
158: }
159:
160: page.setProjectInternal(project);
161:
162: template.init(page, map, width, height);
163: Iterator<Box> iter = template.iterator();
164: while (iter.hasNext()) {
165: page.getBoxes().add(iter.next());
166: }
167:
168: ProjectExplorer.getProjectExplorer().open(page);
169: ProjectExplorer.getProjectExplorer().selectReveal(
170: new StructuredSelection(page));
171:
172: }
173:
174: /**
175: * TODO summary sentence for setActiveEditor ...
176: *
177: * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction,
178: * org.eclipse.ui.IEditorPart)
179: * @param action
180: * @param targetEditor
181: */
182: public void setActiveEditor(IAction action, IEditorPart targetEditor) {
183: }
184:
185: /**
186: * TODO summary sentence for selectionChanged ...
187: *
188: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
189: * org.eclipse.jface.viewers.ISelection)
190: * @param action
191: * @param selection
192: */
193: public void selectionChanged(IAction action, ISelection selection) {
194: }
195: }
|