/** Reads all relevant extensions. */ private void extensionReader() { final IExtensionRegistry registry = Platform.getExtensionRegistry(); for (final IConfigurationElement ce : registry.getConfigurationElementsFor(InternalConstants.SCRIPT_ENGINES_EXTENSION_POINT)) { final String elementName = ce.getName(); if (elementName.equals(InternalConstants.ENGINE_TAG)) { final String language = ce.getAttribute(InternalConstants.LANGUAGE_TAG); if (language == null || language.length() == 0) { LogUtils.error( ce, InternalConstants.LANGUAGE_TAG + " must be specified. Ignored"); // $NON-NLS-1$ continue; } if (getEngines().get(language) != null) { LogUtils.error(ce, "Duplicate declaration of language '" + language + "'. Ignored."); continue; } final IScriptEngineDescriptor engine = IScriptEngineFactory.eINSTANCE.createScriptEngineDescriptor(); engine.init(language, ce); getEngines().put(language, engine); } else { LogUtils.error(ce, "Unknown tag: '" + ce.getName() + "'"); } } }
@Override public void selectionChanged(SelectionChangedEvent event) { if (Activator.getDefault().TRACE_SOURCE_PROVIDER_VERBOSE) { LogUtils.debug(this, "new selection: " + event.getSelection()); if (event.getSelection().isEmpty()) { LogUtils.debug(this, "empty"); } } reportSourceChanges(myPreviousValueEvent); }
/** * Returns the object for the holder object. * * @return the object or <code>null</code> */ public Class<X> getObjectClass() { final String name = myCE.getContributor().getName(); final String className = myCE.getAttribute(myAttrName); final Bundle bundle = Activator.getDefault().getBundle(name); if (bundle == null) { LogUtils.error(myCE, myAttrName + ": cannot find bundle configuration element"); return null; } try { return (Class<X>) bundle.loadClass(className); } catch (final ClassNotFoundException ex) { LogUtils.error(myCE, myAttrName + ": class cannot be loaded: " + className, ex); return null; } }
@Override public void addItem(int id, ToolItem item) { if (item.getParent() != myToolBar) { LogUtils.error(this, "Item does nopt have the correct toolbar as parent. Disposed."); item.dispose(); return; } if (myItems.containsKey(id)) { LogUtils.error(this, "item ID " + id + " already used. Item Disposed."); item.dispose(); return; } myItems.put(id, item); }
/** * Returns the specified type of image. * * <p>All images are registered in the image registry of the {@link Activator}. * * @param type either {@link ICommandImageService#TYPE_DEFAULT} or {@link * ICommandImageService#TYPE_DISABLED} * @return the image */ private Image getImage(int type) { String key = myCommandId; switch (type) { case ICommandImageService.TYPE_DEFAULT: key += "-default"; break; case ICommandImageService.TYPE_DISABLED: key += "-disabled"; break; case ICommandImageService.TYPE_HOVER: key += "-hover"; break; default: LogUtils.error(this, "Unknown type: " + type); break; } if (getToolBarImages().containsKey(key)) return getToolBarImages().get(key); final ImageDescriptor id = myCommandImageService.getImageDescriptor( myCommandId, type, ICommandImageService.IMAGE_STYLE_TOOLBAR); if (id == null) { getToolBarImages().put(key, null); return null; } final ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry(); imageRegistry.put(key, id); final Image image = imageRegistry.get(key); TOOLBAR_IMAGES.put(key, image); return image; }
@Override public void dispose() { if (Activator.getDefault().TRACE_SOURCE_MODEL) { LogUtils.debug( getGrid().getModel(), "Model[" + getGrid().getGrid().hashCode() + "]: dispose[" + getColumn().getId() + ", " + getRow().getId() + "]@" + hashCode()); } if (getLabelUIAttribute() instanceof VirtualUIAttribute) { ((AbstractUIAttribute) getLabelUIAttribute()).removeChangeListener(myPropertyValueListener); } getLabelBinding().dispose(); /* * Before we break the connection */ if (getGrid().getFocusCell() == this) { getGrid().setFocusCell((IGridBindingCellInformation) null); } setColumn(null); setRow(null); }
@Override public void commandChanged(CommandEvent commandEvent) { myItem.setEnabled(myCommand.getCommand().isEnabled()); try { myItem.setToolTipText(myCommand.getCommand().getDescription()); } catch (final NotDefinedException ex) { LogUtils.error(this, ex); } }
/** * Returns the object for the holder object. * * @return the object or <code>null</code> */ public X getObject() { if (myObject == null) { try { myObject = (X) myCE.createExecutableExtension(myAttrName); } catch (final CoreException ex) { LogUtils.error(myCE, ex); } } return myObject; }
@Override public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { if (Activator.getDefault().TRACE_PROPERTY_TESTERS) { LogUtils.debug(this, Constants.PREFIX + property + "(" + receiver + ")"); } if (!(receiver instanceof IManager)) { LogUtils.error(this, "Receiver not IManager: " + receiver); return false; } if (Constants.PROPERTY_CAN_UNDO.equals(property)) return EditingDomainUtils.getCommandStack().canUndo(); if (Constants.PROPERTY_CAN_REDO.equals(property)) return EditingDomainUtils.getCommandStack().canRedo(); if (Constants.PROPERTY_IS_SAVE_NEEDED.equals(property)) return EditingDomainUtils.getCommandStack().isSaveNeeded(); return false; }
private CommandToolItemAdapter(String commandId) { myCommandId = commandId; myItem = new ToolItem(getToolBar(), SWT.PUSH); try { myCommand = myCommandService.deserialize(commandId); myCommand.getCommand().addCommandListener(this); myItem.setImage(getImage(ICommandImageService.TYPE_DEFAULT)); myItem.setDisabledImage(getImage(ICommandImageService.TYPE_DISABLED)); myItem.setHotImage(getImage(ICommandImageService.TYPE_HOVER)); commandChanged(null); } catch (final NotDefinedException ex) { LogUtils.error(this, ex); } catch (final SerializationException ex) { LogUtils.error(this, ex); } myItem.addListener(SWT.Dispose, this); myItem.addListener(SWT.Selection, this); }
@Override public void handleEvent(Event event) { switch (event.type) { case SWT.Selection: try { myHandlerService.executeCommand(myCommand, event); } catch (final ExecutionException ex) { LogUtils.error(this, ex); } catch (final NotDefinedException ex) { LogUtils.error(this, ex); } catch (final NotEnabledException ex) { LogUtils.error(this, ex); } catch (final NotHandledException ex) { LogUtils.error(this, ex); } break; case SWT.Dispose: dispose(); break; default: break; } }
/** Updates this cell. */ protected void updateCellValues() { if (getColumn() == null) { LogUtils.debug(this, "cell disposed! @" + hashCode()); return; } IManagerRunnable.Factory.asyncExec( "update", this, new Runnable() { @Override public void run() { updateCellValuesDelayed(); } }); }
static { ViewerCell vc = null; try { final Constructor<ViewerCell> constructor = ViewerCell.class.getConstructor(ViewerRow.class, Integer.TYPE, Object.class); final boolean wasAccessible = constructor.isAccessible(); try { constructor.setAccessible(true); vc = constructor.newInstance(null, 0, null); } finally { constructor.setAccessible(wasAccessible); } } catch (final Exception ex) { LogUtils.error(null, "Cannot create ViewerCell", ex); } myActivationEventViewerCell = vc; }
/** * Checks if any current {@link IScriptDependency} match the specified notification. * * <p>If one does match, all associated expressions are re-evaluated. * * @param msg the notification to check */ protected void checkChangedDependencies(Notification msg) { final EList<IScriptDependency> dList = getDependencies().get(msg.getNotifier()); /* * Should probably never happen! */ if (dList == null) return; for (final IScriptDependency d : dList) { if (d.getFeature() != msg.getFeature()) { continue; } if (d.getIndex() != -1) { if (d.getIndex() != msg.getPosition()) { continue; } } if (d.getKey() != null) { // TODO LogUtils.debug(this, "key=" + d.getKey() + "\nmsg=" + ToStringUtils.toString(msg)); } /* * We have a match... */ for (final IScriptExpression e : d.getExpressions()) { IManagerRunnable.Factory.asyncExec( "evaluate", e, new Runnable() { @Override public void run() { e.evaluate(); } }); } } }
/** * Returns a Map with the current state for the specific event if specified. * * @param event the current event - possibly <code>null</code> * @return a Map with the current state */ public Map<String, Object> getCurrentState(final Event event) { final Map<String, Object> map = new HashMap<String, Object>(); final List<IObservableValue> values = new ArrayList<IObservableValue>(); resetMap(map); if (event.type == SWT.MenuDetect) { // LogUtils.debug(event, "MenuDetect"); } try { myLastBinding = IBindingContext.Factory.getBindingForWidget(event.widget); if (myLastBinding == null) return map; map.put(Constants.SOURCES_ACTIVE_CONTEXT, myLastBinding.getContext()); final ISourceProviderStateContext context = new ISourceProviderStateContext() { @Override public Event getEvent() { return event; } private Point myLocation = null; @Override public Point getLocation() { if (myLocation == null) { myLocation = new Point(event.x, event.y); switch (event.type) { case SWT.MenuDetect: /* * The location is relative to the display */ myLocation = event.widget.getDisplay().map(null, (Control) event.widget, myLocation); break; default: break; } } return myLocation; } @Override public Map<String, Object> getState() { return map; } @Override public void putSourceValue(String name, Object value) { map.put(name, value); } @Override public void addObservedValue(IObservableValue value) { values.add(value); } @Override public void setSelectionProvider(ISelectionProvider provider) { if (provider == myCurrentSelectionProvider) return; if (myCurrentSelectionProvider != null) { myCurrentSelectionProvider.removeSelectionChangedListener( myCurrentSelectionProviderListener); } myCurrentSelectionProvider = provider; if (myCurrentSelectionProvider != null) { myCurrentSelectionProvider.addSelectionChangedListener( myCurrentSelectionProviderListener); } } }; try { myLastBinding.updateSourceProviderState(context); } catch (final Exception ex) { LogUtils.error(myLastBinding, ex); } /* * If the active binding is a value binding, run all extenders as well... */ final Object activeBinding = map.get(Constants.SOURCES_ACTIVE_BINDING); if (activeBinding instanceof IValueBinding) { final IValueBinding vb = (IValueBinding) activeBinding; for (final IUIBindingDecoratorExtenderDescriptor d : IManager.Factory.getManager().getDecoratorExtenders()) { final CEObjectHolder<IUIBindingDecoratorExtender> factory = d.getFactory(); final IUIBindingDecoratorExtender extender = factory.getObject(); if (extender == null) { LogUtils.error(factory.getConfigurationElement(), "Cannot create extender"); continue; } try { if (!extender.isEnabled(vb)) { continue; } extender.updateSourceProviderState(vb, context); } catch (final Exception ex) { LogUtils.error(factory.getConfigurationElement(), ex); } } } observe(event, values); } catch (final Exception ex) { LogUtils.error(this, ex); } return map; }
/** * Initializes the cell with the information from the model. * * @param column the column * @param row the row */ public void init(IGridBindingColumnInformation column, IGridBindingRowInformation row) { setColumn(column); setRow(row); final IGridBinding grid = getGrid(); final IBindingContext context = grid.getContext(); final IGridModel model = grid.getModel(); final Object columnID = column.getId(); final Object rowID = row.getId(); // LogUtils.debug(this, "\ncolumn=" + columnID + "\nrow=" + rowID); IGridCell cell = null; try { if (Activator.getDefault().TRACE_SOURCE_MODEL) { LogUtils.debug( model, "Model[" + grid.getGrid().hashCode() + "]: getcell(" + columnID + ", " + rowID + ")"); } cell = model.getCell(columnID, rowID); if (Activator.getDefault().TRACE_SOURCE_MODEL) { LogUtils.debug(model, "Model: >>> " + cell); } } catch (final Exception ex) { LogUtils.throwException(this, "Cannot get cell", ex); } try { if (Activator.getDefault().TRACE_SOURCE_MODEL) { LogUtils.debug(model, "Model[" + grid.getGrid().hashCode() + "]: getValue"); } final IObservableValue value = cell.getValue(); if (Activator.getDefault().TRACE_SOURCE_MODEL) { LogUtils.debug(model, "Model: >>> " + value); } /* * Retrieve the wanted information */ setObjectValue(value); /* * If we didn't get a IOV, then just ignore it... */ if (value == null) return; // TODO: set the renderer: final item.s setDataType(IBindingDataType.Factory.create(value.getValue(), value.getValueType())); /* * We will use a UI binding to convert from myValue to myLabelUIAttribute... */ final AbstractUIAttribute attribute = new VirtualUIAttribute(String.class); setLabelUIAttribute(attribute); final IValueBinding lb = context.addBinding().model(value).ui(attribute); final Map<String, Object> args = cell.getArguments(); if (args != null) { lb.args(args); } lb.setCell(this); setLabelBinding(lb); /* * Set up the painter */ final Grid gridControl = grid.getGrid(); final UIAttributePainter p = new UIAttributePainter(gridControl, attribute); p.setDefaultBackground(gridControl.getBackground()); setPainter(p); /* * The column header sets the column width based on the arguments of the label binding */ if (rowID == IGridModel.HEADER1) { final GridColumn gridColumn = column.getGridColumn(); gridColumn.setWidth(lb.getArgument(Constants.ARG_WIDTH, Integer.class, 60)); // TODO: cell renderer // TODO: alignment } /* * We added a new binding so call finish as well... */ context.finish(FinishOption.IF_ALREADY_FINISHED); attribute.addChangeListener(myPropertyValueListener); // An immediate update updateCellValuesDelayed(); } catch (final Exception ex) { LogUtils.error(this, ex); } finally { cell.dispose(); } }
/** * Creates a list mapper that corresponding to the facilities of the specified type. * * <p>The mapper is chosen according to the following priorities: * * <ul> * <li>annotated field * <li>via genmodel label field - requires additional generation of information * <li>by field name: label, name, fullName * <li>by field type: String * <li>{@link #toString()} * </ul> * * @param binding the binding to map * @param ec the class to create the mapper for * @return the mapper */ public static IClassIdentiferMapper createClassIdentiferMapper(IBinding binding, EClass ec) { EStructuralFeature feature; /* * Any constant string is used first of all... * * TODO: Problem: if text is specified, icon and other arguments are ignored! */ final String constantText = binding.getArgument(Constants.ARG_TEXT, String.class, null); if (constantText != null) return new IClassIdentiferMapper() { @Override public Object map(Object value) { return constantText; } @Override public IObservableValue getObservableValue( IObservableValue value, EditingDomain editingDomain) { return value; } }; // Via Annotation final String featureNames = binding.getArgument(Constants.ARG_FEATURE_NAME, String.class, null); if (featureNames != null) { // Pattern.compile("(\\p{L}+)") final StringTokenizer st = new StringTokenizer(featureNames, "."); EClassifier c = ec; final List<EStructuralFeature> sfs = new ArrayList<EStructuralFeature>(); while (st.hasMoreTokens()) { final String name = st.nextToken(); binding.assertTrue(c instanceof EClass, "Intermidiate features must be references"); final EClass eClass = (EClass) c; feature = eClass.getEStructuralFeature(name); binding.assertTrue( feature != null, "Unknown feature: '" + eClass.getName() + "." + name + "' in argument " + Constants.ARG_FEATURE_NAME + " (" + featureNames + ")"); sfs.add(feature); c = feature.getEType(); } switch (sfs.size()) { case 0: LogUtils.error( binding, "Feature names '" + featureNames + "' does not exist. Ignored.", binding.getCreationPoint()); break; case 1: return new SingleFeatureMapper(sfs.get(0)); default: return new MultipleFeatureMapper(sfs.toArray(new EStructuralFeature[sfs.size()])); } } // By Field Name feature = ec.getEStructuralFeature("label"); if (feature != null) return new SingleFeatureMapper(feature); feature = ec.getEStructuralFeature("name"); if (feature != null) return new SingleFeatureMapper(feature); feature = ec.getEStructuralFeature("fullName"); if (feature != null) return new SingleFeatureMapper(feature); // By Field Type for (final EAttribute a : ec.getEAllAttributes()) { if (a.getEType() == EcorePackage.Literals.ESTRING) return new SingleFeatureMapper(a); } // Fall back on toString().... return DEFAULT_MAPPER; }
/** * Checks if the current state have changed, and reports these. * * @param event the current event * @return the resulting map */ public Map<String, Object> reportSourceChanges(Event event) { if (Activator.getDefault().TRACE_SOURCE_PROVIDER_VERBOSE && Activator.getDefault().TRACE_EVENTS_SWT) { LogUtils.debug( this, (event == myPreviousValueEvent ? "REPLAY " : "") + TSSWTUtils.toString(event)); } /* * If the event is FocusIn for the very same widget as last time, but with x,y=0,0, then... * ignore it... It is seen whenever the current shell is send to front. */ if (event.type == SWT.FocusIn && event.widget == myLastWidget && event.x == 0 && event.y == 0) return myCurrentState; /* * Ignore all key up events, except those that navigate... */ // if (event.type == SWT.KeyUp && (SWT.KEYCODE_BIT & event.keyCode) == 0) { // return myOldState; // } final Map<String, Object> newState = getCurrentState(event); myLastWidget = event.widget; /* * Update the current state with changes - keeping them in newState as well. */ for (final Iterator<Map.Entry<String, Object>> is = newState.entrySet().iterator(); is.hasNext(); ) { final Map.Entry<String, Object> i = is.next(); final String s = i.getKey(); final Object n = i.getValue(); final Object o = myCurrentState.get(s); if (BasicUtils.equals(n, o)) { is.remove(); } else { myCurrentState.put(s, n); } } if (!newState.isEmpty()) { /* * Reset the property testers as well, when any of the values changes */ newState.put(Constants.PREFIX + Constants.PROPERTY_CAN_DELETE, true); newState.put(Constants.PREFIX + Constants.PROPERTY_CAN_DELETE_SELECTED_OBJECTS, true); if (Activator.getDefault().TRACE_SOURCE_PROVIDER) { final StringBuilder sb = new StringBuilder("Binding sources change(" + myLastBinding + ")"); for (final Map.Entry<String, Object> i : newState.entrySet()) { final String s = i.getKey(); sb.append("\n ").append(s).append('='); final Object v = i.getValue(); if (v == null) { sb.append("<null>"); } else if (v == IEvaluationContext.UNDEFINED_VARIABLE) { sb.append("<undef>"); } else { sb.append('\'') .append(v.toString()) .append('\'') .append(" [") .append(ClassUtils.getLastClassName(v)) .append(']'); } } LogUtils.debug(this, sb.toString()); } fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION, newState); /* * TODO: describe why * * TODO: only when changing? */ final Object activeBindingObject = myCurrentState.get(Constants.SOURCES_ACTIVE_BINDING); if (activeBindingObject instanceof IValueBinding) { final IValueBinding vb = (IValueBinding) activeBindingObject; IManagerRunnable.Factory.asyncExec( "update", vb, new Runnable() { @Override public void run() { if (vb.isDisposed()) return; vb.updateBinding(); } }); } /* * And lastly, update the active contexts */ handleContextChanges(myCurrentState); } return myCurrentState; }
/** @param newState */ private void handleContextChanges(final Map<String, Object> newState) { /* * If inside a container binding, then activate the proper context */ final boolean cb = newState.get(Constants.SOURCES_ACTIVE_CONTAINER_BINDING) != IEvaluationContext.UNDEFINED_VARIABLE; if (cb && myContainerContextContextActivation == null) { myContainerContextContextActivation = myContextService.activateContext(Constants.CONTAINER_CONTEXT_ID); if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "activated " + Constants.CONTAINER_CONTEXT_ID); } } if (!cb && myContainerContextContextActivation != null) { myContextService.deactivateContext(myContainerContextContextActivation); myContainerContextContextActivation = null; if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "deactivated " + Constants.CONTAINER_CONTEXT_ID); } } /* * If inside an value binding, then activate the proper context */ final boolean vb = !cb && newState.get(Constants.SOURCES_ACTIVE_BINDING) != IEvaluationContext.UNDEFINED_VARIABLE; if (vb && myWidgetContextContextActivation == null) { myWidgetContextContextActivation = myContextService.activateContext(Constants.WIDGET_CONTEXT_ID); if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "activated " + Constants.WIDGET_CONTEXT_ID); } } if (!vb && myWidgetContextContextActivation != null) { myContextService.deactivateContext(myWidgetContextContextActivation); myWidgetContextContextActivation = null; if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "deactivated " + Constants.WIDGET_CONTEXT_ID); } } /* * Also activate the base context - this does not seem to happen automatically ??? */ final boolean bc = myContainerContextContextActivation != null || myWidgetContextContextActivation != null; if (bc && myBaseContextContextActivation == null) { myBaseContextContextActivation = myContextService.activateContext(Constants.COMMON_CONTEXT_ID); if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "activated " + Constants.COMMON_CONTEXT_ID); } } if (!bc && myBaseContextContextActivation != null) { myContextService.deactivateContext(myBaseContextContextActivation); myBaseContextContextActivation = null; if (Activator.getDefault().TRACE_CONTEXTS) { LogUtils.debug(this, "deactivated " + Constants.COMMON_CONTEXT_ID); } } }