001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.tool.display;
018:
019: import java.text.MessageFormat;
020: import java.util.List;
021:
022: import net.refractions.udig.project.ui.internal.Messages;
023:
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.events.SelectionEvent;
027: import org.eclipse.swt.events.SelectionListener;
028: import org.eclipse.swt.graphics.Image;
029: import org.eclipse.swt.widgets.Display;
030: import org.eclipse.swt.widgets.Menu;
031: import org.eclipse.swt.widgets.MenuItem;
032: import org.eclipse.swt.widgets.ToolBar;
033: import org.eclipse.swt.widgets.ToolItem;
034:
035: /**
036: * Abstract class for contribution item that represent the current
037: * item selected on the toolbar.
038: *
039: * @author jeichar
040: * @since 0.9.0
041: */
042: public abstract class AbstractToolbarContributionItem extends
043: CurrentContributionItem {
044:
045: ToolItem toolItem;
046:
047: /**
048: * Current modal item that is represented by this contribution in UI.
049: */
050: protected ModalItem currentTool;
051:
052: private boolean checked;
053:
054: /**
055: * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
056: */
057: public final void fill(final ToolBar parent, int index) {
058: if (getTools().size() == 0)
059: return;
060:
061: toolItem = createToolItem(parent, index);
062:
063: setCurrentTool(getTools().get(0));
064:
065: if (isActiveItem() || (isDefaultItem()
066: // && ToolProxy.activeItem == null
067: )) {
068: runCurrentTool();
069: }
070:
071: toolItem.addSelectionListener(new SelectionListener() {
072:
073: public void widgetSelected(SelectionEvent e) {
074: widgetDefaultSelected(e);
075: }
076:
077: public void widgetDefaultSelected(SelectionEvent e) {
078: if (e.detail == SWT.ARROW) {
079: final Menu menu = new Menu(parent);
080: for (final ModalItem tool : getTools()) {
081: MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
082: menuItem.setImage(tool.getImage());
083: menuItem.setText(tool.getName());
084: menuItem.setEnabled(tool.isEnabled());
085:
086: menuItem
087: .addSelectionListener(new SelectionListener() {
088:
089: public void widgetSelected(
090: SelectionEvent e) {
091: widgetDefaultSelected(e);
092: }
093:
094: public void widgetDefaultSelected(
095: SelectionEvent e) {
096: setCurrentTool(tool);
097: runCurrentTool();
098: menu.dispose();
099: }
100: });
101: }
102: menu.setVisible(true);
103: } else {
104: runCurrentTool();
105: }
106: }
107:
108: });
109:
110: }
111:
112: /**
113: * If the tool is the default tool then run that tool.
114: *
115: * @return true if the default tool is in the current category and the default category should
116: * be activated.
117: */
118: protected abstract boolean isDefaultItem();
119:
120: protected abstract boolean isActiveItem();
121:
122: /**
123: * Creates the ToolItem to add to the toolbar
124: *
125: * @param parent
126: * @param index
127: */
128: protected abstract ToolItem createToolItem(ToolBar parent, int index);
129:
130: /**
131: * returns the list of items for the category.
132: *
133: * @return
134: */
135: protected abstract List<ModalItem> getTools();
136:
137: /**
138: * Sets the current items from the category as the current items for the tool item
139: *
140: * @param tool the new current tool. It must be one of the items in the category.
141: */
142: protected void setCurrentTool(final ModalItem tool) {
143: if (currentTool == tool)
144: return;
145: currentTool = tool;
146: if (Display.getCurrent() == null) {
147: Display.getDefault().asyncExec(new Runnable() {
148: public void run() {
149: setWidgetState(tool);
150:
151: }
152: });
153: } else
154: setWidgetState(tool);
155:
156: }
157:
158: /**
159: *
160: * @param tool
161: */
162: private void setWidgetState(ModalItem tool) {
163: if (!isDisposed() && toolItem != null) {
164: if (toolItem.getImage() == null)
165: toolItem.setImage(tool.getImage());
166: toolItem.setToolTipText(tool.getToolTipText());
167: }
168: }
169:
170: /**
171: * Selects passed modal tool if the <code>toolItem</code> widget has been created
172: * and not disposed. Otherwise just ignore selection until the widget will be created.
173: *
174: *
175: * @see net.refractions.udig.project.ui.internal.tool.display.CurrentContributionItem#setSelection(boolean,
176: * net.refractions.udig.project.ui.internal.tool.display.ModalItem)
177: */
178: public void setSelection(boolean checked, ModalItem tool) {
179: if (toolItem != null && !toolItem.isDisposed()) {
180: setCurrentTool(tool);
181: if (checked) {
182: Image activeImage = tool.getActiveImage();
183: if (activeImage != null)
184: toolItem.setImage(activeImage);
185: } else {
186: Image image = tool.getImage();
187: if (image != null)
188: toolItem.setImage(image);
189: }
190: this .checked = checked;
191: }
192: }
193:
194: /**
195: * @see net.refractions.udig.project.ui.internal.tool.display.CurrentContributionItem#isChecked()
196: */
197: protected boolean isChecked() {
198: return checked;
199: }
200:
201: /**
202: * Runs the current tool.
203: */
204: protected void runCurrentTool() {
205: if (currentTool != null) {
206: // if( !currentTool.isEnabled() ) {
207: // for( ModalItem item: getTools() ) {
208: // if( item.isEnabled() ){
209: // currentTool=item;
210: // break;
211: // }
212: // }
213: // }
214: if (currentTool.isEnabled())
215: currentTool.run();
216: else {
217: final Display disp = Display.getDefault();
218: MessageDialog
219: .openWarning(
220: disp.getActiveShell(),
221: Messages.AbstractToolbarContributionItem_warning_title,
222: MessageFormat
223: .format(
224: Messages.AbstractToolbarContributionItem_warning_message,
225: new Object[] { currentTool
226: .getName() }));
227: }
228: }
229: }
230:
231: /**
232: * @see net.refractions.udig.project.ui.internal.tool.display.CurrentContributionItem#isDisposed()
233: */
234: public boolean isDisposed() {
235: return toolItem != null && toolItem.isDisposed();
236: }
237:
238: /**
239: * Sets the current to too be the next tool in the list.
240: */
241: public void incrementSelection() {
242: if (!isEnabledTool())
243: return;
244: int index = getTools().indexOf(currentTool);
245: if (index == getTools().size() - 1)
246: index = 0;
247: else
248: index++;
249: ModalItem modalItem = getTools().get(index);
250: setCurrentTool(modalItem);
251: if (!modalItem.isEnabled())
252: incrementSelection();
253: }
254:
255: /**
256: * Returns true if one of the tools in the category is enabled.
257: *
258: * @return true if one of the tools in the category is enabled.
259: */
260: private boolean isEnabledTool() {
261: for (ModalItem item : getTools()) {
262: if (item.isEnabled())
263: return true;
264: }
265: return false;
266: }
267:
268: // public boolean isEnabled() {
269: // return enabled;
270: // }
271: //
272: // public void setEnabled( boolean enabled ) {
273: // this.enabled = enabled;
274: // }
275:
276: }
|