0001: package net.refractions.udig.style.ui;
0002:
0003: import java.util.ArrayList;
0004: import java.util.Comparator;
0005: import java.util.HashSet;
0006: import java.util.Iterator;
0007: import java.util.List;
0008: import java.util.Set;
0009: import java.util.TreeSet;
0010:
0011: import net.refractions.udig.core.internal.ExtensionPointProcessor;
0012: import net.refractions.udig.core.internal.ExtensionPointUtil;
0013: import net.refractions.udig.project.ILayerListener;
0014: import net.refractions.udig.project.LayerEvent;
0015: import net.refractions.udig.project.internal.Layer;
0016: import net.refractions.udig.style.IStyleConfigurator;
0017: import net.refractions.udig.style.Images;
0018: import net.refractions.udig.style.StylePlugin;
0019: import net.refractions.udig.style.internal.Messages;
0020: import net.refractions.udig.style.internal.StyleLayer;
0021: import net.refractions.udig.style.internal.StyleManager;
0022:
0023: import org.eclipse.core.runtime.IAdaptable;
0024: import org.eclipse.core.runtime.IConfigurationElement;
0025: import org.eclipse.core.runtime.IExtension;
0026: import org.eclipse.core.runtime.IStatus;
0027: import org.eclipse.core.runtime.Status;
0028: import org.eclipse.jface.action.Action;
0029: import org.eclipse.jface.action.ControlContribution;
0030: import org.eclipse.jface.action.GroupMarker;
0031: import org.eclipse.jface.action.IAction;
0032: import org.eclipse.jface.action.IContributionItem;
0033: import org.eclipse.jface.action.IMenuManager;
0034: import org.eclipse.jface.action.IStatusLineManager;
0035: import org.eclipse.jface.action.IToolBarManager;
0036: import org.eclipse.jface.action.MenuManager;
0037: import org.eclipse.jface.action.Separator;
0038: import org.eclipse.jface.action.ToolBarManager;
0039: import org.eclipse.jface.viewers.ISelection;
0040: import org.eclipse.jface.viewers.ISelectionProvider;
0041: import org.eclipse.jface.viewers.IStructuredSelection;
0042: import org.eclipse.swt.SWT;
0043: import org.eclipse.swt.events.SelectionEvent;
0044: import org.eclipse.swt.events.SelectionListener;
0045: import org.eclipse.swt.layout.FillLayout;
0046: import org.eclipse.swt.layout.FormAttachment;
0047: import org.eclipse.swt.layout.FormData;
0048: import org.eclipse.swt.layout.FormLayout;
0049: import org.eclipse.swt.widgets.Combo;
0050: import org.eclipse.swt.widgets.Composite;
0051: import org.eclipse.swt.widgets.Control;
0052: import org.eclipse.swt.widgets.Label;
0053: import org.eclipse.swt.widgets.Shell;
0054: import org.eclipse.ui.IActionBars;
0055: import org.eclipse.ui.IEditorPart;
0056: import org.eclipse.ui.IKeyBindingService;
0057: import org.eclipse.ui.IMemento;
0058: import org.eclipse.ui.ISelectionListener;
0059: import org.eclipse.ui.IViewSite;
0060: import org.eclipse.ui.IWorkbenchPage;
0061: import org.eclipse.ui.IWorkbenchPart;
0062: import org.eclipse.ui.IWorkbenchWindow;
0063: import org.eclipse.ui.PartInitException;
0064: import org.eclipse.ui.part.PageBook;
0065: import org.eclipse.ui.part.ViewPart;
0066: import org.eclipse.ui.services.IServiceLocator;
0067:
0068: /**
0069: * Style Editing View.
0070: * <p>
0071: * StyleView is responsible for allowing the user to choose between applicable StyleConfigurators
0072: * for the current blackboard.
0073: * </p>
0074: * <p>
0075: * What does this mean?
0076: * <ul>
0077: * <li>Listens to any workbench selection and will engage when a Layer is selected
0078: * <li>Will use both the Layer.getResource and StyleBlackboard when figuring out which
0079: * StyleConfigurators are applicable
0080: * <li>Will display a select control in the viewpart toolbar if their is more then one to choose
0081: * from, if there is only one a Label showing the StyleConfigurator name will be shown.
0082: * <li>The StyleConfigurator will be supplied with the Layer & Blackboard to edit, note this is
0083: * *not* the same black board as used by the Layer for live rendering!
0084: * <li>Is responsible for applying any changes to the Blackboard, this is done using an apply
0085: * button on the viewpart toolbar. Changes will also be applied when the workbench looses focus on
0086: * the layer.
0087: * </ul>
0088: * </p>
0089: * TODO: Clone the blackboard and give that to the configurator
0090: *
0091: * @author jdeolive
0092: * @since 0.5
0093: */
0094: public class StyleView extends ViewPart implements StyleManager {
0095: /** ID used in the extention point to identify this view */
0096: public final static String VIEW_ID = "net.refractions.udig.style.styleView"; //$NON-NLS-1$
0097:
0098: private final static String STYLE_MENU_GROUP = "style"; //$NON-NLS-1$
0099: private final static String CONFIG_MENU_GROUP = "config"; //$NON-NLS-1$
0100: private final static String CHOOSER_MENU_GROUP = "chooser"; //$NON-NLS-1$
0101:
0102: /**
0103: * Choose which styleConfigurator to use.
0104: * <p>
0105: * This is contributed to the toolbar via an IContributionItem.
0106: * </p>
0107: */
0108: Combo configChooser;
0109:
0110: /** Page book used to switch between available StyleConfigurators */
0111: PageBook book;
0112: Label pleaseSelectLayer;
0113:
0114: private Action applyAction;
0115: private Action cancelAction;
0116:
0117: // private IContributionItem applyCI;
0118: // private IContributionItem cancelCI;
0119:
0120: // private HashMap<Layer, StyleLayer> layer2styleLayer = new HashMap<Layer,StyleLayer>();
0121:
0122: /** Current layer being worked on (wrapped as a StyleLayer) or null if we don't have a victim * */
0123: private StyleLayer currentLayer;
0124:
0125: /**
0126: * List of StyleViewSites each one manages a IStyleConfigurator.
0127: * <p>
0128: * Note: This list should be accessed via getStyleConfigurators, that method will only show you
0129: * sites that are applicable to the curernt layer.
0130: * </p>
0131: */
0132: List<StyleViewSite> sites;
0133: private IStyleConfigurator currentConfig;
0134:
0135: /**
0136: * Watch workbench selection, any Layer anywhere must be styled.
0137: * <p>
0138: * Latches onto the first Layer found.
0139: * </p>
0140: * <p>
0141: * Will call setCurrentLayer based on what is found.
0142: * </p>
0143: */
0144: private ISelectionListener workbenchWatcher = new ISelectionListener() {
0145: public void selectionChanged(IWorkbenchPart part,
0146: ISelection selection) {
0147: if (part == StyleView.this )
0148: return;
0149:
0150: if (selection instanceof IStructuredSelection) {
0151: IStructuredSelection sselection = (IStructuredSelection) selection;
0152:
0153: // look for a Layer selection(s)
0154: for (Iterator itr = sselection.iterator(); itr
0155: .hasNext();) {
0156: Object obj = itr.next();
0157: if (obj instanceof Layer) {
0158: Layer layer = (Layer) obj;
0159: setCurrentLayer(layer);
0160:
0161: // System.out.println("Selected layer "+layer );
0162: return;
0163: }
0164: }
0165: }
0166: }
0167: };
0168: SelectionListener chooserListener = new SelectionListener() {
0169: /** Change current page in book */
0170: public void widgetSelected(SelectionEvent e) {
0171: if (configChooser != null
0172: && configChooser.getSelectionIndex() > -1) {
0173: int index = configChooser.getSelectionIndex();
0174: IStyleConfigurator config = new ArrayList<IStyleConfigurator>(
0175: getStyleConfigurators()).get(index);
0176: // System.out.println("You have selected "+index+" aka "+config );
0177: setStyleConfigurator(config);
0178: return;
0179: }
0180: // not found!
0181: // System.out.println("You have selected .. nothing!" ); //$NON-NLS-1$
0182: book.showPage(pleaseSelectLayer);
0183: }
0184:
0185: /**
0186: * aka double click in a list, return in chooser
0187: * <p>
0188: * Makes a call to applyStyle?
0189: * </p>
0190: */
0191: public void widgetDefaultSelected(SelectionEvent e) {
0192: // book.showPage( (Control) e.data );
0193: // applyStyle();
0194: }
0195: };
0196: ILayerListener layerListener = new ILayerListener() {
0197: public void refresh(LayerEvent event) {
0198: StyleView.this .refresh();
0199: }
0200: };
0201:
0202: /**
0203: * Construct <code>StyleView</code>.
0204: * <p>
0205: * Note since we are a view - nothing much happens here.
0206: * <ul>
0207: * <li>init will be called allowing us to grab our prefs
0208: * <li>createPartControl control will be called allowing us to set up before display
0209: * <li>dispose will be called when we are closed
0210: * </ul>
0211: * </p>
0212: */
0213: public StyleView() {
0214: super ();
0215: }
0216:
0217: /**
0218: * Called before createPartControl to give us chance to organize ourselves.
0219: * <p>
0220: * We used this to latch onto the defined StyleConfigurators.
0221: * </p>
0222: *
0223: * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
0224: */
0225: public void init(IViewSite site, IMemento memento)
0226: throws PartInitException {
0227: super .init(site, memento);
0228: if (sites != null) { // horrible must be a mistake
0229: throw new IllegalStateException(
0230: "StyleView init called twice!"); //$NON-NLS-1$
0231: }
0232: sites = new ArrayList<StyleViewSite>();
0233:
0234: ExtensionPointUtil.process(StylePlugin.getDefault(),
0235: IStyleConfigurator.XPID, new ExtensionPointProcessor() {
0236: public void process(IExtension extension,
0237: IConfigurationElement element)
0238: throws Exception {
0239: IStyleConfigurator config = (IStyleConfigurator) element
0240: .createExecutableExtension("class"); //$NON-NLS-1$
0241:
0242: String id = element.getAttribute("styleId"); //$NON-NLS-1$
0243: String label = element.getAttribute("label"); //$NON-NLS-1$
0244: config.setStyleId(id);
0245: config.setLabel(label);
0246:
0247: StyleViewSite styleSite = new StyleViewSite(
0248: extension, element, config);
0249: try {
0250: config.init(styleSite);
0251: sites.add(styleSite);
0252: } catch (Exception e) {
0253: IStatus status = new Status(IStatus.ERROR,
0254: element.getNamespace(), IStatus.OK,
0255: null, e);
0256: StylePlugin.getDefault().getLog().log(
0257: status);
0258: } catch (Throwable t) {
0259: IStatus status = new Status(
0260: IStatus.ERROR,
0261: element.getNamespace(),
0262: IStatus.OK,
0263: "Could not create " + element.getName(), t); //$NON-NLS-1$
0264: StylePlugin.getDefault().getLog().log(
0265: status);
0266: // could not process element
0267: }
0268: }
0269: });
0270: }
0271:
0272: /**
0273: * Creates the style editor layout, and uses a PageBook placeholder for ui widgets to be placed
0274: * into as Styles are selected.
0275: *
0276: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
0277: * @param parent
0278: */
0279: public void createPartControl(Composite parent) {
0280: createActions();
0281: createToolBar();
0282:
0283: parent.setLayout(new FormLayout());
0284:
0285: FormData layout;
0286: layout = new FormData();
0287: layout.top = new FormAttachment(null, 0);
0288: layout.left = new FormAttachment(null, 1);
0289: layout.width = 200;
0290:
0291: book = new PageBook(parent, SWT.NONE);
0292: layout = new FormData();
0293: layout.top = new FormAttachment(0);
0294: layout.left = new FormAttachment(0);
0295: layout.right = new FormAttachment(100);
0296: layout.bottom = new FormAttachment(100);
0297: book.setLayoutData(layout);
0298:
0299: pleaseSelectLayer = new Label(book, SWT.DEFAULT);
0300: pleaseSelectLayer.setAlignment(SWT.LEFT);
0301: pleaseSelectLayer
0302: .setText(Messages.StyleView_label_selectLayer_text);
0303: book.showPage(pleaseSelectLayer);
0304:
0305: // Add in all the config controls as pages in the book
0306: for (StyleViewSite site : sites) {
0307: Composite page = new Composite(book, SWT.NONE);
0308: page.setLayout(new FillLayout());
0309: try {
0310: site.createControl(page);
0311: // note this may result in a call to StyleViewPart.getToolbarManager
0312: // or something.
0313: } catch (Throwable t) {
0314:
0315: t.printStackTrace();
0316: // TODO log exception
0317: page.dispose();
0318: site.dispose();
0319: sites.remove(this );
0320: }
0321: }
0322:
0323: // listen to selections from the workbench
0324: IWorkbenchPage page2 = getSite().getPage();
0325: page2.addSelectionListener(workbenchWatcher);
0326:
0327: IWorkbenchPage page = getViewSite().getPage();
0328: workbenchWatcher.selectionChanged(page.getActivePart(), page
0329: .getSelection());
0330:
0331: // Find the current layer
0332: ISelection sel = page2.getSelection();
0333: if (findLayer(sel))
0334: return;
0335:
0336: IEditorPart activeEditor = page2.getActiveEditor();
0337: if (activeEditor != null) {
0338: sel = activeEditor.getSite().getSelectionProvider()
0339: .getSelection();
0340: if (findLayer(sel))
0341: return;
0342: }
0343:
0344: sel = page2
0345: .getSelection("net.refractions.udig.project.ui.layerManager"); //$NON-NLS-1$
0346: if (findLayer(sel))
0347: return;
0348: sel = page2
0349: .getSelection("net.refractions.udig.project.ui.projectExplorer"); //$NON-NLS-1$
0350: if (findLayer(sel))
0351: return;
0352:
0353: }
0354:
0355: private boolean findLayer(ISelection sel) {
0356: Layer layer = null;
0357: if (sel == null || sel.isEmpty())
0358: return false;
0359:
0360: if (!(sel instanceof IStructuredSelection))
0361: return false;
0362:
0363: IStructuredSelection selection = (IStructuredSelection) sel;
0364: for (Iterator iter = selection.iterator(); iter.hasNext();) {
0365: Object e = iter.next();
0366: if (e instanceof Layer) {
0367: layer = (Layer) e;
0368: break;
0369: }
0370: if (e instanceof IAdaptable) {
0371: IAdaptable adapter = (IAdaptable) e;
0372: Object adapted = adapter.getAdapter(Layer.class);
0373: if (adapted != null) {
0374: layer = (Layer) adapted;
0375: break;
0376: }
0377: }
0378: }
0379:
0380: if (layer != null) {
0381: setCurrentLayer(layer);
0382: return true;
0383: }
0384: return false;
0385: }
0386:
0387: /**
0388: * New layer, or null if there is no selected layer.
0389: */
0390: public void setCurrentLayer(Layer layer) {
0391: if (currentLayer == null && layer == null) {
0392: return;
0393: }
0394: if (layer != null && layer.equals(currentLayer)) {
0395: return;
0396: }
0397: if (currentLayer != null) {
0398: currentLayer.removeListener(layerListener);
0399: }
0400: if (layer == null) {
0401: currentLayer = null;
0402: } else {
0403: currentLayer = new StyleLayer(layer);
0404: currentLayer.addListener(layerListener);
0405: }
0406: enableActions(currentLayer != null);
0407:
0408: // Check if the current site still works
0409: //
0410: IStyleConfigurator config = null;
0411: if (layer == null) {
0412: config = null;
0413: } else if (currentConfig != null
0414: && currentConfig.canStyle(layer)) {
0415: config = currentConfig;
0416: } else {
0417: for (StyleViewSite site : sites) {
0418: if (site.getConfig().canStyle(layer)) {
0419: config = site.getConfig();
0420: break;
0421: }
0422: }
0423: }
0424: setStyleConfigurator(config);
0425: // let's update the chooser
0426: //
0427: updateChooser();
0428: }
0429:
0430: /**
0431: * Update chooser to reflect getStyleConfigurators list and currentConfig.
0432: */
0433: void updateChooser() {
0434: if (configChooser == null)
0435: return; // chooser is not created yet
0436:
0437: List<String> items = new ArrayList<String>();
0438:
0439: // populate the list with each of the configurators labels, or id if null
0440: //
0441: Set<IStyleConfigurator> configs = getStyleConfigurators();
0442: for (IStyleConfigurator config : configs) {
0443: String label = config.getLabel();
0444: items.add(label);
0445: }
0446:
0447: // update the chooser list
0448: configChooser.setItems(items.toArray(new String[items.size()]));
0449:
0450: if (configChooser.getItemCount() == 0) {
0451: // no items, set a default message
0452: String message = Messages.StyleView_chooser_message;
0453: configChooser.add(message);
0454: configChooser.setEnabled(false);
0455: configChooser.setText(message);
0456: return;
0457: } else if (configChooser.getItemCount() == 1) {
0458: // one item, set first as active
0459: configChooser.setVisibleItemCount(1);
0460: configChooser.setEnabled(true);
0461: } else {
0462: configChooser
0463: .setVisibleItemCount(Math.min(5, items.size()));
0464: configChooser.setEnabled(true);
0465: }
0466:
0467: if (currentConfig == null) {
0468: configChooser.setText("--"); //$NON-NLS-1$
0469: } else {
0470: configChooser.setText(currentConfig.getLabel());
0471: }
0472: }
0473:
0474: public Layer getCurrentLayer() {
0475: return currentLayer;
0476: }
0477:
0478: /**
0479: * Acquire configurators for the current layer.
0480: * <p>
0481: * <ul>
0482: * <li>WARNING: this method is only valid to call *after* init has been called.
0483: * <li>ARNING: these IStyleConfigurators will only have their ui available after
0484: * createPartControl has completed. Please don't call setFocus( IStyleConfigurator ) before
0485: * hand.
0486: * </ul>
0487: * </p>
0488: * <p>
0489: * This means you should *not* assume these IStyleConfigurators are totally happy and ready to
0490: * work. They will only listen to events when they are the current page for example.
0491: * </p>
0492: *
0493: * @return Set of configurators for the current layer, may be empty
0494: */
0495: public Set<IStyleConfigurator> getStyleConfigurators() { // FIXME - make this a List!
0496: Layer layer = getCurrentLayer();
0497:
0498: Set<IStyleConfigurator> set = new TreeSet<IStyleConfigurator>(
0499: new Comparator<IStyleConfigurator>() {
0500: public int compare(IStyleConfigurator a,
0501: IStyleConfigurator b) {
0502: if (a == b)
0503: return 0;
0504: if (a == null || a.getLabel() == null)
0505: return -1;
0506: if (b == null || b.getLabel() == null)
0507: return 1;
0508: return a.getLabel().compareTo(b.getLabel());
0509: }
0510: });
0511:
0512: if (layer == null || sites == null || sites.size() == 0)
0513: return set;
0514:
0515: Set<IStyleConfigurator> badSites = new HashSet<IStyleConfigurator>();
0516: for (StyleViewSite site : sites) {
0517: try {
0518: IStyleConfigurator config = site.getConfig();
0519: if (config.canStyle(layer)) {
0520: set.add(config);
0521: }
0522: } catch (Throwable t) {
0523: t.printStackTrace();
0524: // site was bad and must die!
0525: StylePlugin.getDefault().getLog().log(
0526: new Status(IStatus.INFO, site.getPluginId(),
0527: IStatus.OK, null, t));
0528: badSites.add(site.config);
0529: }
0530: }
0531: if (!badSites.isEmpty())
0532: sites.removeAll(badSites);
0533: return set;
0534: }
0535:
0536: /**
0537: * Current AbstractStyleConfigurator or null if we don't have one
0538: * <p>
0539: * Grab the IStyleConfigurator
0540: *
0541: * @return IStyleConfigurator public IStyleConfigurator getCurrent(){ int index =
0542: * chooser.getSelectionIndex(); if( index == -1 ) return null; Set<IStyleConfigurator>
0543: * set = getStyleConfigurators( ); if( set == null || set.isEmpty() || set.size() <
0544: * index ) return null; return new ArrayList<IStyleConfigurator>( set ).get(index); }
0545: */
0546:
0547: /**
0548: * Focuses the style configurator.
0549: */
0550: void focusConfigurator(IStyleConfigurator config) {
0551: if (currentLayer == null)
0552: return;
0553: config.focus(currentLayer);
0554:
0555: /*
0556: * StyleLayer styleLayer = layer2styleLayer.get(currentLayer); if (styleLayer == null) {
0557: * styleLayer = new StyleLayer(currentLayer); layer2styleLayer.put(currentLayer,
0558: * styleLayer); } config.focus(styleLayer);
0559: */
0560: }
0561:
0562: /**
0563: * Set focus to the chooser if available.
0564: * <p>
0565: * TODO: Should set the focus to the current StyleView being displayed.
0566: * </p>
0567: */
0568: public void setFocus() {
0569: if (configChooser != null) {
0570: configChooser.setFocus();
0571: }
0572: }
0573:
0574: /**
0575: * Set the current StyleConfigurator used by this StyleView to the provided config.
0576: * <p>
0577: * Note if there is only one chooser, we should set the focus to the styleConfigurator.
0578: * </p>
0579: * <p>
0580: * Responsibilities:
0581: * <ul>
0582: * <li>set the chooser text to the config.getLabel!
0583: * <li>call site.focus() for the config - so the page gets show, and the toolbar gets shown
0584: * </ul>
0585: *
0586: * @param config IStyleConfigurator to be displayed by StyleView
0587: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
0588: */
0589: public void setStyleConfigurator(IStyleConfigurator config) {
0590: currentConfig = config;
0591: /*
0592: * if( configChooser != null && config != null ){ configChooser.setText( config.getLabel() ); }
0593: */
0594: if (currentConfig != null) {
0595: currentConfig.setAction(applyAction);
0596: for (StyleViewSite site : sites) {
0597: if (site.getConfig() == currentConfig) {
0598: // System.out.println( "I am trying to show "+site +" for "+config );
0599: site.focus(); // show this site
0600: return;
0601: }
0602: }
0603: }
0604: book.showPage(pleaseSelectLayer);
0605: // should hide toolbar?
0606: }
0607:
0608: /**
0609: * This *is* the current styleConfigurator used by this style view.
0610: * <p>
0611: * The following controls need to be kept in sync:
0612: * <ul>
0613: * <li>configChooser (if it exists yet) needs to use getStyleConfigurator().getLabel as its
0614: * text
0615: * <li>book needs to use StyleViewSite page associated with this StyleConfigurator
0616: * <li>the toolbar from the StyleViewSite also needs to be displayed
0617: * </ul>
0618: *
0619: * @return IStyleConfigurator currently being displayed
0620: */
0621: public IStyleConfigurator getStyleConfigurator() {
0622: return currentConfig;
0623: }
0624:
0625: private void enableActions(boolean enable) {
0626: applyAction.setEnabled(enable);
0627: cancelAction.setEnabled(enable);
0628: }
0629:
0630: private void createActions() {
0631: /*
0632: * Handles input when user presses Apply button. Alerts the current configurator to apply
0633: * the style to the layer.
0634: */
0635: applyAction = new Action("apply") { //$NON-NLS-1$
0636: public void run() {
0637: apply();
0638: }
0639: };
0640: applyAction.setEnabled(false);
0641: applyAction.setToolTipText(Messages.StyleView_apply_tooltip);
0642: applyAction.setImageDescriptor(Images
0643: .getDescriptor(ImageConstants.APPLY_STYLE));
0644: // applyCI = new ActionContributionItem(applyAction);
0645:
0646: cancelAction = new Action("cancel") { //$NON-NLS-1$
0647: public void run() {
0648: revert();
0649: }
0650: };
0651: cancelAction.setEnabled(false);
0652: cancelAction.setToolTipText(Messages.StyleView_cancel_tooltip);
0653: cancelAction.setImageDescriptor(Images
0654: .getDescriptor(ImageConstants.CANCEL_STYLE));
0655: // cancelCI = new ActionContributionItem(cancelAction);
0656: }
0657:
0658: void apply() {
0659: if (currentLayer == null)
0660: return;
0661: currentLayer.apply();
0662: /*
0663: * StyleLayer styleLayer = layer2styleLayer.get(currentLayer); if (styleLayer == null)
0664: * return; styleLayer.apply();
0665: */
0666: }
0667:
0668: void revert() {
0669: if (currentLayer == null)
0670: return;
0671:
0672: currentLayer.revert();
0673: refresh();
0674: /*
0675: * StyleLayer styleLayer = layer2styleLayer.get(currentLayer); if (styleLayer == null)
0676: * return; styleLayer.revert(); //refresh everyone for (StyleViewSite site : sites) {
0677: * site.config.focus(currentLayer); }
0678: */
0679: }
0680:
0681: /**
0682: * Refresh all the viewsites, aka force stylecon figurators to reset.
0683: */
0684: public void refresh() {
0685: for (StyleViewSite site : sites) {
0686: site.config.focus(currentLayer);
0687: }
0688: }
0689:
0690: private void createToolBar() {
0691: IToolBarManager mgr = getViewSite().getActionBars()
0692: .getToolBarManager();
0693:
0694: mgr.add(new GroupMarker(CHOOSER_MENU_GROUP));
0695: mgr.appendToGroup(CHOOSER_MENU_GROUP, new ControlContribution(
0696: "none") { //$NON-NLS-1$
0697: protected Control createControl(Composite parent) {
0698: configChooser = new Combo(parent, SWT.DROP_DOWN
0699: | SWT.READ_ONLY);
0700:
0701: // update the chooser with the currently selected layer
0702: //
0703: updateChooser();
0704:
0705: configChooser
0706: .addSelectionListener(chooserListener);
0707:
0708: // setFocus();
0709: // updateChooser();
0710: /*
0711: * XXX Justin what are you doing?
0712: * PlatformUI.getWorkbench().getDisplay().asyncExec( new Runnable() { public
0713: * void run() { chooserListener.widgetSelected(null); } } );
0714: */
0715: return configChooser;
0716: }
0717:
0718: protected int computeWidth(Control control) {
0719: return 125;
0720: }
0721: });
0722: mgr.add(new GroupMarker(STYLE_MENU_GROUP));
0723: mgr.appendToGroup(STYLE_MENU_GROUP, new Separator());
0724: mgr.appendToGroup(STYLE_MENU_GROUP, applyAction);
0725: mgr.appendToGroup(STYLE_MENU_GROUP, cancelAction);
0726:
0727: mgr.add(new GroupMarker(CONFIG_MENU_GROUP));
0728: mgr.add(new Separator());
0729: }
0730:
0731: /**
0732: * Note: createPartControl may not even of been called
0733: *
0734: * @see org.eclipse.ui.part.WorkbenchPart#dispose()
0735: */
0736: public void dispose() {
0737: if (sites != null) {
0738: for (StyleViewSite site : sites) {
0739: try {
0740: site.dispose();
0741: } catch (Throwable t) {
0742: // problem cleaning up after site
0743: }
0744: }
0745: }
0746: if (workbenchWatcher != null) {
0747: getSite().getPage().removeSelectionListener(
0748: workbenchWatcher);
0749: }
0750: super .dispose();
0751: }
0752:
0753: /**
0754: * This is the "site" for the IStyleConfigurator - it provides context for the part to work
0755: * against.
0756: *
0757: * @author jgarnett
0758: * @since 0.9.0
0759: */
0760: class StyleViewSite implements IViewSite {
0761: String idPlugin;
0762: String idExtention;
0763: String idStyle;
0764: IActionBars actionBars;
0765:
0766: /** Config being managed */
0767: IStyleConfigurator config;
0768:
0769: /** Page holding the config ui, page of book */
0770: private Composite page;
0771:
0772: /**
0773: * Page holding the toolbar, page of toolBook ToolBar toolbar;
0774: */
0775:
0776: ToolBarManager toolbarManager;
0777:
0778: /**
0779: * Grabs a viewsite so we can talk to and manage resources associated with the part.
0780: *
0781: * @param extention extention point being processed
0782: * @param element element providing content
0783: * @param part
0784: */
0785: public StyleViewSite(IExtension extention,
0786: IConfigurationElement element, IStyleConfigurator part) {
0787: idPlugin = element.getNamespace();
0788: idExtention = extention.getUniqueIdentifier();
0789: idStyle = part.getStyleId();
0790: config = part;
0791: actionBars = null; // will create only if config asks for it
0792: // toolbar = null;
0793: toolbarManager = null; // toolbar created by our actionBars
0794: }
0795:
0796: /**
0797: * Call config.createControl with a empty page from book.
0798: *
0799: * @param parent
0800: */
0801: public void createControl(Composite parent) {
0802: config.createControl(parent);
0803:
0804: page = parent;
0805: }
0806:
0807: public IActionBars getActionBars() {
0808: if (actionBars != null)
0809: return actionBars;
0810:
0811: final IActionBars global = getViewSite().getActionBars();
0812: actionBars = new IActionBars() {
0813: public void clearGlobalActionHandlers() {
0814: global.clearGlobalActionHandlers();
0815: }
0816:
0817: public IAction getGlobalActionHandler(String actionId) {
0818: return global.getGlobalActionHandler(actionId);
0819: }
0820:
0821: public IMenuManager getMenuManager() {
0822: return null;
0823: }
0824:
0825: public IStatusLineManager getStatusLineManager() {
0826: return global.getStatusLineManager();
0827: }
0828:
0829: public IToolBarManager getToolBarManager() {
0830: if (toolbarManager == null) {
0831: toolbarManager = new ToolBarManager() {
0832: public void update(boolean force) {
0833: super .update(force);
0834: /*
0835: * IToolBarManager gtbm = global.getToolBarManager(); //remove all
0836: * the items in the config group IContributionItem[] items =
0837: * gtbm.getItems(); boolean remove = false; for (int i = 0; i <
0838: * items.length; i++) { if (items[i].isGroupMarker()) { GroupMarker
0839: * marker = (GroupMarker)items[i]; remove =
0840: * marker.getId().equals(CONFIG_MENU_GROUP); } else { if (remove) {
0841: * gtbm.remove(items[i]); } } } //add items to the config group
0842: * items = getItems(); for (int i = 0; i < items.length; i++) {
0843: * gtbm.appendToGroup(CONFIG_MENU_GROUP, items[i]); }
0844: * gtbm.update(false);
0845: */
0846: }
0847: };
0848: }
0849: return toolbarManager;
0850: }
0851:
0852: public void setGlobalActionHandler(String actionId,
0853: IAction handler) {
0854: global.setGlobalActionHandler(actionId, handler);
0855: }
0856:
0857: public void updateActionBars() {
0858: global.updateActionBars();
0859: }
0860:
0861: public IServiceLocator getServiceLocator() {
0862: return global.getServiceLocator();
0863: }
0864: };
0865: return actionBars;
0866: }
0867:
0868: /**
0869: * Returns the secondary id for this part site's part, or <code>null</code> if it has
0870: * none.
0871: */
0872: public String getSecondaryId() {
0873: return idStyle;
0874: }
0875:
0876: /**
0877: * Returns the part registry extension id for this part.
0878: * <p>
0879: * The name comes from the <code>id</code> attribute in the configuration element.
0880: * </p>
0881: *
0882: * @return the registry extension id
0883: */
0884: public String getId() {
0885: return idExtention;
0886: }
0887:
0888: /**
0889: * Returns the unique identifier of the plug-in that defines this workbench site's part.
0890: *
0891: * @return the unique identifier of the declaring plug-in
0892: */
0893: public String getPluginId() {
0894: return idPlugin;
0895: }
0896:
0897: /*
0898: * Returns the registered name for this workbench site's part. <p> The name comes from the
0899: * <code>label</code> attribute in the configuration element. </p>
0900: */
0901: public String getRegisteredName() {
0902: return config.getLabel();
0903: }
0904:
0905: public void registerContextMenu(String menuId,
0906: MenuManager menuManager,
0907: ISelectionProvider selectionProvider) {
0908: // nope!
0909: }
0910:
0911: public void registerContextMenu(MenuManager menuManager,
0912: ISelectionProvider selectionProvider) {
0913: // nope!
0914: }
0915:
0916: public IKeyBindingService getKeyBindingService() {
0917: return null;
0918: }
0919:
0920: public IWorkbenchPage getPage() {
0921: return StyleView.this .getViewSite().getPage();
0922: }
0923:
0924: public ISelectionProvider getSelectionProvider() {
0925: return StyleView.this .getViewSite().getSelectionProvider();
0926: }
0927:
0928: public Shell getShell() {
0929: return StyleView.this .getViewSite().getShell();
0930: }
0931:
0932: public IWorkbenchWindow getWorkbenchWindow() {
0933: return StyleView.this .getViewSite().getWorkbenchWindow();
0934: }
0935:
0936: public void setSelectionProvider(ISelectionProvider provider) {
0937: StyleView.this .getViewSite().setSelectionProvider(provider);
0938: }
0939:
0940: public Object getAdapter(Class adapter) {
0941: return null;
0942: }
0943:
0944: /**
0945: * Grab config object associated with the site.
0946: * <p>
0947: * Note this object may be lazy loaded.
0948: * </p>
0949: *
0950: * @return Config object for the site
0951: */
0952: public IStyleConfigurator getConfig() {
0953: return config;
0954: }
0955:
0956: /**
0957: * Clean up after site - will at least call config.dispose() if needed.
0958: */
0959: public void dispose() {
0960: if (page != null && !page.isDisposed()) {
0961: page.dispose();
0962: page = null;
0963: }
0964: /*
0965: * if( toolbar != null && !page.isDisposed()){ toolbar.dispose(); toolbar = null; }
0966: */
0967: if (config != null) {
0968: config.dispose();
0969: config = null;
0970: }
0971: }
0972:
0973: /**
0974: * Focus on this style configurator.
0975: * <p>
0976: * This needs to:
0977: * <ul>
0978: * <li>Show the page for this site
0979: * <li>Show the toolbars for this site
0980: * </ul>
0981: */
0982: public void focus() {
0983: if (currentLayer == null) {
0984: // run screaming!
0985: book.showPage(pleaseSelectLayer);
0986: return;
0987: }
0988: if (!getConfig().canStyle(currentLayer)) {
0989: throw new IllegalStateException(
0990: "Trying to use " + config //$NON-NLS-1$
0991: + " with a layer it cannot style"); //$NON-NLS-1$
0992: }
0993: if (page == null || page.isDisposed()) {
0994: throw new IllegalStateException(
0995: "Framework should have called createPartControl before trying to focus"); //$NON-NLS-1$
0996: }
0997: getConfig().focus(currentLayer); // smack config with layer to display
0998:
0999: book.setVisible(true);
1000: book.showPage(page);
1001: page.setVisible(true);
1002:
1003: // How do I grab the toolbar for this site toolbar?
1004: // FIXME: help with sub toolbar
1005: if (toolbarManager != null) {
1006: IToolBarManager mgr = getViewSite().getActionBars()
1007: .getToolBarManager();
1008: mgr.add(new GroupMarker(CONFIG_MENU_GROUP));
1009: for (IContributionItem item : toolbarManager.getItems()) {
1010: mgr.appendToGroup(CONFIG_MENU_GROUP, item);
1011: }
1012: } else {
1013: IToolBarManager mgr = getViewSite().getActionBars()
1014: .getToolBarManager();
1015: mgr.removeAll();
1016: createToolBar();
1017: mgr.remove(CONFIG_MENU_GROUP);
1018: // mgr.add(new GroupMarker(CONFIG_MENU_GROUP));
1019: }
1020: getViewSite().getActionBars().updateActionBars();
1021: }
1022:
1023: /*
1024: * @see java.lang.Object#toString()
1025: */
1026: public String toString() {
1027: return "Site<" + config.getLabel() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
1028: }
1029:
1030: public IWorkbenchPart getPart() {
1031: return StyleView.this ;
1032: }
1033:
1034: public Object getService(Class api) {
1035: // TODO3.2 Auto-generated method stub
1036: return null;
1037: }
1038:
1039: public boolean hasService(Class api) {
1040: // TODO3.2 Auto-generated method stub
1041: return false;
1042: }
1043: }
1044: }
|