001: /**
002: * <copyright>
003: * </copyright>
004: *
005: * $Id: ToolContextImpl.java 22197 2006-10-17 16:48:42Z jeichar $
006: */package net.refractions.udig.project.ui.internal.tool.impl;
007:
008: import java.awt.Rectangle;
009:
010: import net.refractions.udig.project.command.Command;
011: import net.refractions.udig.project.command.EditCommand;
012: import net.refractions.udig.project.command.MapCommand;
013: import net.refractions.udig.project.command.NavCommand;
014: import net.refractions.udig.project.command.factory.BasicCommandFactory;
015: import net.refractions.udig.project.command.factory.EditCommandFactory;
016: import net.refractions.udig.project.command.factory.NavigationCommandFactory;
017: import net.refractions.udig.project.command.factory.SelectionCommandFactory;
018: import net.refractions.udig.project.internal.impl.AbstractContextImpl;
019: import net.refractions.udig.project.internal.render.RenderManager;
020: import net.refractions.udig.project.ui.commands.DrawCommandFactory;
021: import net.refractions.udig.project.ui.commands.IDrawCommand;
022: import net.refractions.udig.project.ui.internal.MapEditor;
023: import net.refractions.udig.project.ui.internal.MapEditorSite;
024: import net.refractions.udig.project.ui.internal.tool.ToolContext;
025: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
026:
027: import org.eclipse.core.runtime.Plugin;
028: import org.eclipse.core.runtime.Status;
029: import org.eclipse.jface.action.IStatusLineManager;
030: import org.eclipse.swt.widgets.Display;
031: import org.eclipse.ui.IActionBars2;
032: import org.eclipse.ui.IEditorPart;
033: import org.eclipse.ui.IEditorSite;
034: import org.eclipse.ui.IWorkbench;
035: import org.eclipse.ui.IWorkbenchPage;
036: import org.eclipse.ui.IWorkbenchWindow;
037: import org.eclipse.ui.PlatformUI;
038:
039: /**
040: * <p>
041: * The following features are implemented:
042: * <ul>
043: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getContextModel <em>Context Model</em>}</li>
044: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getPixelSize <em>Pixel Size</em>}</li>
045: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getViewportModel <em>Viewport Model</em>}</li>
046: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getEditManager <em>Edit Manager</em>}</li>
047: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getRenderManager <em>Render Manager</em>}</li>
048: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getMapDisplay <em>Map Display</em>}</li>
049: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getMap <em>Map</em>}</li>
050: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getProject <em>Project</em>}</li>
051: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getViewportPane <em>Viewport Pane</em>}</li>
052: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getDrawFactory <em>Draw Factory</em>}</li>
053: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getEditFactory <em>Edit Factory</em>}</li>
054: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getNavigationFactory <em>Navigation Factory</em>}</li>
055: * <li>{@link net.refractions.udig.project.ui.internal.tool.impl.ToolContextImpl#getSelectionFactory <em>Selection Factory</em>}</li>
056: * </ul>
057: * </p>
058: *
059: */
060: public class ToolContextImpl extends AbstractContextImpl implements
061: ToolContext {
062:
063: /**
064: * The cached value of the '{@link #getDrawFactory() <em>Draw Factory</em>}' attribute.
065: *
066: * @see #getDrawFactory()
067: */
068: private final DrawCommandFactory drawFactory = DrawCommandFactory
069: .getInstance();
070:
071: /**
072: * The cached value of the '{@link #getEditFactory() <em>Edit Factory</em>}' attribute.
073: *
074: * @see #getEditFactory()
075: */
076: private final EditCommandFactory editFactory = EditCommandFactory
077: .getInstance();
078:
079: /**
080: * The cached value of the '{@link #getNavigationFactory() <em>Navigation Factory</em>}'
081: * attribute.
082: *
083: * @see #getNavigationFactory()
084: */
085: private final NavigationCommandFactory navigationFactory = NavigationCommandFactory
086: .getInstance();
087:
088: /**
089: * The cached value of the '{@link #getSelectionFactory() <em>Selection Factory</em>}'
090: * attribute.
091: *
092: * @see #getSelectionFactory()
093: */
094: private final SelectionCommandFactory selectionFactory = SelectionCommandFactory
095: .getInstance();
096:
097: private final BasicCommandFactory basicCommandFactory = BasicCommandFactory
098: .getInstance();
099:
100: public ToolContextImpl() {
101: super ();
102: }
103:
104: public ToolContextImpl(ToolContextImpl impl) {
105: super (impl);
106: }
107:
108: public DrawCommandFactory getDrawFactory() {
109: return drawFactory;
110: }
111:
112: public EditCommandFactory getEditFactory() {
113: return editFactory;
114: }
115:
116: public NavigationCommandFactory getNavigationFactory() {
117: return navigationFactory;
118: }
119:
120: public SelectionCommandFactory getSelectionFactory() {
121: return selectionFactory;
122: }
123:
124: public void sendCommand(IDrawCommand command) {
125: command.setMap(getMap());
126: ViewportPane viewportPane = ((ViewportPane) getRenderManagerInternal()
127: .getMapDisplay());
128: viewportPane.addDrawCommand(command);
129: Rectangle validArea;
130: try {
131: validArea = command.getValidArea();
132: } catch (Exception e) {
133: validArea = null;
134: }
135: if (validArea != null)
136: viewportPane.repaint(validArea.x, validArea.y,
137: validArea.width, validArea.height);
138: else
139: viewportPane.repaint();
140: }
141:
142: public void sendASyncCommand(Command command) {
143: if (command instanceof NavCommand)
144: getMapInternal().sendCommandASync((NavCommand) command);
145: else if (command instanceof EditCommand)
146: getMapInternal().sendCommandASync((EditCommand) command);
147: else if (command instanceof IDrawCommand)
148: sendCommand((IDrawCommand) command);
149: else if (command instanceof MapCommand)
150: getMapInternal().sendCommandASync((MapCommand) command);
151: else
152: getProjectInternal().sendASync(command);
153: }
154:
155: public void sendSyncCommand(Command command) {
156: if (command instanceof NavCommand)
157: getMapInternal().sendCommandSync((NavCommand) command);
158: else if (command instanceof EditCommand)
159: getMapInternal().sendCommandSync((EditCommand) command);
160: else if (command instanceof IDrawCommand)
161: sendCommand((IDrawCommand) command);
162: else if (command instanceof MapCommand)
163: getMapInternal().sendCommandSync((MapCommand) command);
164: else
165: getProjectInternal().sendSync(command);
166: }
167:
168: public ViewportPane getViewportPane() {
169: return (ViewportPane) getMapDisplay();
170: }
171:
172: public IWorkbench getWorkbench() {
173: return PlatformUI.getWorkbench();
174: }
175:
176: public Display getDisplay() {
177: return Display.getDefault();
178: }
179:
180: public void log(Plugin currentPlugin, String message, int severity,
181: Throwable exception) {
182: String ID1 = currentPlugin.getBundle().getSymbolicName();
183: currentPlugin.getLog().log(
184: new Status(severity, ID1, 0, message, exception));
185: }
186:
187: @Override
188: public RenderManager getRenderManagerInternal() {
189: return getMapInternal().getRenderManagerInternal();
190: }
191:
192: public IStatusLineManager getStatusBar() {
193: MapEditorSite site = getEditorSite();
194: if (site == null)
195: return null;
196: return site.getActionBars().getStatusLineManager();
197: }
198:
199: public IActionBars2 getActionBars() {
200: IEditorSite site = getEditorSite();
201: if (site == null)
202: return null;
203: return (IActionBars2) site.getActionBars();
204: }
205:
206: private MapEditorSite getEditorSite() {
207: IWorkbenchWindow window = getWindow();
208: if (window == null)
209: return null;
210: IWorkbenchPage page = window.getActivePage();
211: if (page == null)
212: return null;
213: IEditorPart part = page.getActiveEditor();
214: if (part == null || !(part instanceof MapEditor))
215: return null;
216: return ((MapEditor) part).getMapEditorSite();
217: }
218:
219: private IWorkbenchWindow getWindow() {
220: IWorkbench bench = PlatformUI.getWorkbench();
221: if (bench == null)
222: return null;
223: IWorkbenchWindow window = bench.getActiveWorkbenchWindow();
224: if (window == null) {
225: if (bench.getWorkbenchWindowCount() > 0)
226: window = bench.getWorkbenchWindows()[0];
227: }
228: return window;
229: }
230:
231: public void updateUI(Runnable runnable) {
232: if (Display.getCurrent() != null) {
233: runnable.run();
234: return;
235: }
236: IWorkbench bench = PlatformUI.getWorkbench();
237: Display display = null;
238: if (bench != null)
239: display = bench.getDisplay();
240:
241: if (display == null)
242: display = Display.getDefault();
243:
244: display.asyncExec(runnable);
245: }
246:
247: public BasicCommandFactory getBasicCommandFactory() {
248: return this .basicCommandFactory;
249: }
250:
251: public ToolContextImpl copy() {
252: return new ToolContextImpl(this );
253: }
254: } // Impl
|