Class PropHelper

java.lang.Object
com.iizigo.editor.PropHelper

public class PropHelper extends Object
Helper class for properties.
Author:
Christopher Mindus
  • Field Details

    • MANY_VALUES

      public static final Object MANY_VALUES
      Special object returned for getCommonValue() when many different values exist.
  • Constructor Details

    • PropHelper

      public PropHelper()
  • Method Details

    • getProps

      public static GProp<?>[] getProps(ISelection selection, Class<?>... propClasses)
      Gets the selected properties array. If the selection doesn't contain any properties of the list of possible classes (i.e. has an unknown class), a null array is returned.
      Parameters:
      selection - The selection.
      propClasses - List of classes that are accepted in the returned array.
      Returns:
      The array list of selected properties, or null if the selection contains other elements than properties.
    • doCopy

      public static void doCopy(Shell parent, ISelection selection, Runnable completeCallback)
      Performs the Copy operation. If the selection doesn't contain properties a message box is displayed.
      Parameters:
      parent - The shell.
      selection - The selection in the viewer.
      completeCallback - If non-null, the callback that will be called in the SWT thread when undo completed (e.g. to update states).
    • doPaste

      public static String doPaste(ISelection selection, int position, IPropUndoRedo undoRedo)
      Performs a Paste operation with undo support.
      Parameters:
      selection - The selection in the viewer.
      position - The position in the target (0=inside, -1=before target in parent, 1=after target in parent).
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • doPaste

      public static String doPaste(DesignerProp target, int position, IPropUndoRedo undoRedo)
      Performs a Paste operation with undo support.
      Parameters:
      target - Target property.
      position - The position in the target (0=inside, -1=before target in parent, 1=after target in parent).
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • doPasteLink

      public static String doPasteLink(ISelection selection, IPropUndoRedo undoRedo)
      Performs a Paste-Link operation with undo support.
      Parameters:
      selection - The selection in the viewer.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • doPasteLink

      public static String doPasteLink(DesignerProp target, IPropUndoRedo undoRedo)
      Performs a Paste-Link operation with undo support.
      Parameters:
      target - Target property.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • doAdd

      public static String doAdd(String description, PropCnr target, GProp<?> prop, IPropUndoRedo undoRedo, boolean doAutoRename)
      Adds a property with undo/redo support.
      Parameters:
      description - The description of the operation (if props.length>1, make it in plural).
      target - The target property container.
      prop - The property to add.
      undoRedo - The undo/redo interface.
      doAutoRename - Flag set to true to indicate automatic rename of existing property names.
      Returns:
      The success response is null, otherwise an error message.
    • doAdd

      public static String doAdd(String description, PropCnr target, GProp<?>[] props, IPropUndoRedo undoRedo, boolean doAutoRename)
      Adds properties with undo/redo support.
      Parameters:
      description - The description of the operation (if props.length>1, make it in plural).
      target - The target property container.
      props - The properties.
      undoRedo - The undo/redo interface.
      doAutoRename - Flag set to true to indicate automatic rename of existing property names.
      Returns:
      The success response is null, otherwise an error message.
    • doInsert

      public static String doInsert(String description, PropCnr target, int index, GProp<?>[] props, IPropUndoRedo undoRedo, boolean doAutoRename)
      Adds or inserts properties with undo/redo support.
      Parameters:
      description - The description of the operation (if props.length>1, make it in plural).
      target - The target property container.
      index - The index in target, -1 for none (then position is assumed to be 0, and vice versa).
      props - The properties.
      undoRedo - The undo/redo interface.
      doAutoRename - Flag set to true to indicate automatic rename of existing property names.
      Returns:
      The success response is null, otherwise an error message.
    • executeOperation

      public static String executeOperation(Shell shell, AbstractOperation op)
      Executes an operation.
      Parameters:
      shell - The shell.
      op - The operation.
      Returns:
      null For OK, otherwise an error message that has been displayed in a message box.
    • doDelete

      public static String doDelete(ISelection selection, IPropUndoRedo undoRedo, Runnable completeCallback)
      Deletes the selection in a viewer. If the selection contains a resource, all the selected items must be resources.
      Parameters:
      selection - The selection in the viewer.
      undoRedo - The undo/redo interface.
      completeCallback - If non-null, the callback that will be called in the SWT thread when undo completed (e.g. to update states).
      Returns:
      The success response is null, otherwise an error message.
    • doCut

      public static String doCut(ISelection selection, IPropUndoRedo undoRedo, Runnable completeCallback)
      Cuts the selection in a viewer. If the selection contains a resource, all the selected items must be resources.
      Parameters:
      selection - The selection in the viewer.
      undoRedo - The undo/redo interface.
      completeCallback - If non-null, the callback that will be called in the SWT thread when undo completed (e.g. to update states).
      Returns:
      The success response is null, otherwise an error message.
    • scheduleDeleteJob

      public static void scheduleDeleteJob(String jobName, Shell shell, IResource[] resourcesToDelete, boolean deleteContent, IUndoContext undoContext, Runnable completeCallback)
      Schedules a delete resources job.
    • doDelete

      public static String doDelete(String description, GProp<?>[] props, IPropUndoRedo undoRedo, Runnable completeCallback)
      Performs a delete of pure properties (cannot be resource based). If a property is resource based, an assertion is thrown.
      Parameters:
      description - The description of the operation.
      props - The properties to remove.
      undoRedo - The undo/redo instance.
      completeCallback - Callback called when operation completes, null for none.
      Returns:
      The success response is null, otherwise an error message.
    • doRename

      public static String doRename(ISelection selection, IPropUndoRedo undoRedo)
      Renames the selection in a viewer. The selection must be a single resource based property.
      Parameters:
      selection - The selection in the viewer.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • setValue

      public static String setValue(String description, GProp<?> prop, Object value, IPropUndoRedo undoRedo)
      Sets a single property value.
      Parameters:
      description - The description of the operation.
      prop - The property.
      value - The value.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • setValue

      public static String setValue(String description, PropCnr container, GProp<?> prop, IPropUndoRedo undoRedo) throws PropException
      Sets a single property value in a property container. If the container contains the property, that property is set. If not, the property is added to the container with the set value.
      Parameters:
      description - The description of the operation.
      container - The container.
      prop - The property.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
      Throws:
      PropException - If the value cannot be set in the property.
    • setAttribute

      public static String setAttribute(GProp<?> prop, int attr, boolean flag, IPropUndoRedo undoRedo)
      Sets property attribute.
      Parameters:
      prop - The property.
      attr - The attribute PropAttrEvent.PROP_*
      flag - The flag.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • setError

      public static String setError(GProp<?> prop, String errorID, IPropErrorItem item, IPropUndoRedo undoRedo)
      Sets property error: adds, changes or removes the error, with undo/redo support.
      Parameters:
      prop - The property.
      errorID - The error ID (might be null).
      item - Error item, null to remove the error, otherwise an error instance to set the error.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • setValue

      public static String setValue(String description, GProp<?>[] props, Object[] values, IPropUndoRedo undoRedo)
      Sets property values. The arrays must be of equal lengths.
      Parameters:
      description - The description of the operation.
      props - The array of properties.
      values - The array of values.
      undoRedo - The undo/redo interface.
      Returns:
      The success response is null, otherwise an error message.
    • saveState

      public static void saveState(Viewer viewer, IMemento memento)
      Saves the memento for a Tree or Table Viewer, i.e. the current selection and the expanded items (tree only).
      Parameters:
      viewer - The viewer.
      memento - The memento should be a child of the Editor Part saving the state.
    • restoreState

      public static void restoreState(Viewer viewer, ModuleRootPropCnr root, IMemento memento)
      Restores the state of a viewer once the viewer contents has been set.
      Parameters:
      viewer - The viewer.
      root - The Module root property of the project.
      memento - The memento should be a child of the Editor Part saving the state, can be null.
    • getDesignerProps

      public static ArrayList<DesignerProp> getDesignerProps(GProp<?>[] props)
      Gets the DesignerProps for the specified properties.
    • selectPropsAndFocusTreeViewer

      public static void selectPropsAndFocusTreeViewer(GProp<?>[] props, StructuredViewer viewer)
      Request a selection of the properties. This method may be called from a non-SWT thread, resulting in a synchronous operation call to the SWT thread.
    • setSelection

      public static boolean setSelection(StructuredViewer viewer, DesignerProp dp)
      Sets the selection of the property. Must be from SWT thread and focus is not changed.
      Returns:
      true if selection was set, false for failure.
    • setSelection

      public static int setSelection(StructuredViewer viewer, Collection<DesignerProp> list)
      Sets the selection of the properties. Must be from SWT thread and focus is not changed.
      Returns:
      Count of items selected, zero means failure if list is non-empty.
    • expandAllInPath

      public static void expandAllInPath(StructuredViewer viewer, TreePath path)
      Expands all elements in a path.
      Parameters:
      viewer - The viewer, hopefully a tree viewer.
      path - The path.
    • expandAllInPaths

      public static void expandAllInPaths(StructuredViewer viewer, TreePath[] paths)
      Expands all elements in specified paths.
      Parameters:
      viewer - The viewer, hopefully a tree viewer.
      paths - The paths.
    • getSelectionPath

      public static TreePath getSelectionPath(TreeViewer viewer, Object element)
      Finds the selection path for a DesignerProp in a viewer.
    • getSelectionPath

      public static TreePath getSelectionPath(StructuredViewer viewer, DesignerProp dp)
      Finds the selection path for a DesignerProp in a viewer.
    • setNearestSelection

      public static boolean setNearestSelection(StructuredViewer viewer, DesignerProp dp)
      Sets the nearest selection to the DesignerProp.
      Returns:
      true if selection is found, false otherwise.
    • getNearestSelectionPath

      public static TreePath getNearestSelectionPath(StructuredViewer viewer, DesignerProp dp)
      Finds the selection path for a DesignerProp in a viewer.
    • getSelectionPath

      public static TreePath getSelectionPath(StructuredViewer viewer, DesignerProp dp, HashSet<Object> filtered)
      Finds the selection path for a DesignerProp in a viewer.
    • doRename

      public static boolean doRename(GProp<?> prop, Atom newName, IPropUndoRedo undoRedo) throws PropTypeException
      Performs a rename property with undo/redo support.
      Returns:
      true for change, false for no change.
      Throws:
      PropTypeException - When the property cannot be renamed, same name or read-only.
    • doRename

      public static void doRename(Shell shell, IResource resource, String newName, IUndoContext undoContext)
      Performs a resource rename with undo support.
    • syncExec

      public static void syncExec(GEvent e, EventListener listener)
      Helper routine to execute a property event in the SWT thread synchronously. If the current thread is the SWT thread, the listener is called directly.
    • autoRename

      public static String[] autoRename(PropCnr target, GProp<?>[] props, String first, String next, String next2)
      Auto-renames properties. If the source is from an assets directory and target is not, the file extension is removed if it matches the destination.
      Parameters:
      target - The target container.
      props - The properties.
      first - The first appended string, e.g. "Copy of ".
      next - The next appended string, e.g. "Copy ". The next and next2 are used to append string, e.g. "Copy 2 of NNN".
      next2 - The next #2 appended string, e.g. " of ".
      Returns:
      The string array of the property names.
    • getUndoContext

      public static IUndoContext getUndoContext(GProp<?> gp)
      Gets the undo context for a property.
      Parameters:
      gp - The property.
      Returns:
      The undo context or null for none.
    • addUndoContext

      public static void addUndoContext(GProp<?> gp, IUndoableOperation operation)
      Adds the undo contexts from a property to an operation.
    • addUndoContexts

      public static void addUndoContexts(GProp<?>[] props, IUndoableOperation operation)
      Adds the undo contexts from a property to an operation.
    • getFolder

      public static ModuleFolder getFolder(GProp<?>[] props)
      Gets the common ModuleFolder for properties.
      Returns:
      The common Module Folder, or null for none.
    • getDesignerProp

      public static DesignerProp getDesignerProp(GProp<?> gp)
      Gets a DesignerProp for a property, by searching it and its parents.
    • getCommonParent

      public static PropCnr getCommonParent(GProp<?>[] props)
      Gets the common parent for all properties.
      Parameters:
      props - The properties.
      Returns:
      The common parent, or null if not possible (e.g. different projects).
    • getAncestor

      public static PropCnr getAncestor(GProp<?> gp1, GProp<?> gp2)
      Gets the common ancestor of two properties.
    • doMove

      public static void doMove(DesignerProp base, ISelection selection, IPropUndoRedo undoRedo)
      Performs a Move of properties command by displaying a dialog box to the user so a target can be selected for the move.
    • doMove

      public static String doMove(boolean doPerform, String description, PropCnr target, GProp<?> reference, boolean isAfter, GProp<?>[] props, boolean doAutoRename, IPropUndoRedo undoRedo)
      Performs the move operation of the properties, with auto-renaming as a potential dialog box.
      Parameters:
      doPerform - Flag to perform the operation if true, false to test operation only.
      description - Description of the operation.
      target - Target property.
      reference - Reference property in target where to place the moved properties (can be null if target is not index based or if they should be added last).
      isAfter - Flag indicating how to move the properties in regards to the reference property (when reference is not null).
      props - Properties to move.
      doAutoRename - Auto-rename of properties.
      undoRedo - Undo-redo instance.
      Returns:
      An error message for operation, or null for success.
    • doLink

      public static void doLink(Shell shell, DesignerProp base, ISelection selection)
      Performs a Link of properties command.
    • doOpen

      public static boolean doOpen(boolean doPerform, GProp<?>[] props)
      Performs a Open of properties command. If a property is open in an editor, that editor is placed in focus.
      Parameters:
      doPerform - Set to false to see if this open command would succeed, true to perform the open.
      props - The properties to open in an Editor.
      Returns:
      The success flag, false if none could be opened.
    • doOpen

      public static FilePropCnr doOpen(boolean doPerform, GProp<?> gp, PropEditorPart[] partReturn)
      Opens the Editor associated with the specified property.
      Parameters:
      doPerform - Set to false to see if this open command would succeed, true to perform the open.
      gp - The property to open in an Editor.
      partReturn - Array of 1 in length (minimum), where index 0 is filled upon successful return, or set to null for failure or no need to call doOpen on the part with the property. Set partReturn to null for no return value.
      Returns:
      The success property container opened, null if none could be opened.
    • doOpenAndFocus

      public static boolean doOpenAndFocus(boolean doPerform, GProp<?> gp, int index)
      Opens the selected property in its associated editor, focuses the editor and selects the appropriate property.
      Parameters:
      doPerform - Flag for testing only, or to perform the actual open.
      gp - The property.
      index - Index for a multi-prop, otherwise not used.
      Returns:
      The success property container opened, null if none could be opened.
    • doOpen

      public static IEditorPart doOpen(IFile file)
      Opens the Editor of the specified file.
      Parameters:
      file - The file to open.
      Returns:
      The IEditorPart, if an "IIZI Module Editor", an instance of PropEditorPart. For errors, null is returned.
    • canPerform

      public static String canPerform(GProp<?>[] props, int operation)
      Checks properties for a valid operation.
      Parameters:
      props - The properties.
      operation - The operation: DesignerProp.OP_MOVE or OP_DELETE.
      Returns:
      null for success (operation can be performed), otherwise the error message.
    • canPerform

      public static String canPerform(ArrayList<DesignerProp> list, int operation)
      Checks properties for a valid operation.
      Parameters:
      list - The properties.
      operation - The operation (DesignerProp.OP_*).
    • expandModuleItems

      public static void expandModuleItems(TreeViewer viewer)
      Expand a "standard tree viewer" with all project item of the IIZI Module so that all its files are displayed.
    • expandAll

      public static void expandAll(TreeViewer viewer)
      Expands all items in a "standard tree viewer" that belong to the IIZI Module, i.e. doesn't expand the JRE items that takes forever...
    • validateReferenceNameFormat

      public static String validateReferenceNameFormat(String name, boolean doCheckValidProject, boolean doLookup, Class<?>[] possibleClasses)
      Validates a reference name to be of correct format.
      Parameters:
      name - The reference name to check.
      doCheckValidProject - To check the validity of project.
      doLookup - To look-up for real existing reference.
      possibleClasses - The classes to look-up.
      Returns:
      The error string.
    • doVerify

      public static void doVerify(PropCnr property)
      Verifies the property. This method can only be called for editable properties that are located in a FilePropCnr or a FolderPropCnr, i.e. the data container property. All other properties will generate a logged message and no verification will take place.
      Parameters:
      property - Property to verify.
    • doVerify

      public static void doVerify(PropCnr property, IKStringInfoProvider kstringInfoProvider)
      Verifies the property. This method can only be called for editable properties that are located in a FilePropCnr or a FolderPropCnr, i.e. the data container property. All other properties will generate a logged message and no verification will take place.
      Parameters:
      property - Property to verify.
      kstringInfoProvider - The KStringInfoProvider instance.
      Throws:
      NullPointerException - If kstringInfoProvider is null.
    • queueVerify

      public static boolean queueVerify(PropCnr property)
      Asynchronous-verifies the property in a queue in the SWT thread. This call can be made several times, and the property will only be verified once.

      This method can only be called for editable properties that are located in a FilePropCnr or a FolderPropCnr, i.e. the data container property. All other properties will generate a logged message and no verification will take place.

      Parameters:
      property - Property to verify.
      Returns:
      true if queued, false if already in queue for verification
    • queueVerify

      public static boolean queueVerify(PropCnr property, Runnable callback)
      Asynchronous-verifies the property in a queue in the SWT thread. This call can be made several times, and the property will only be verified once.

      This method can only be called for editable properties that are located in a FilePropCnr or a FolderPropCnr, i.e. the data container property. All other properties will generate a logged message and no verification will take place.

      If there is an existing entry for the property in the queue, nothing will be done and false is returned. Be careful about this as the queued item may have a different callback set! Use doVerify() if this is important!

      Parameters:
      property - Property to verify.
      callback - Callback to be called when verification has been run, null for none.
      Returns:
      true if queued, false if already in queue for verification, property is disposed of, or workbench is closing.
    • queueVerify

      public static boolean queueVerify(PropCnr property, IKStringInfoProvider kstringInfoProvider, Runnable callback)
      Asynchronous-verifies the property in a queue in the SWT thread. This call can be made several times, and the property will only be verified once.

      This method can only be called for editable properties that are located in a FilePropCnr or a FolderPropCnr, i.e. the data container property. All other properties will generate a logged message and no verification will take place.

      If there is an existing entry for the property in the queue, nothing will be done and false is returned. Be careful about this as the queued item may have a different callback set! Use doVerify() if this is important!

      Parameters:
      property - Property to verify.
      kstringInfoProvider - The KStringInfoProvider instance.
      callback - Callback to be called when verification has been run, null for none.
      Throws:
      NullPointerException - If kstringInfoProvider is null.
    • saveViewer

      public static void saveViewer(IMemento memento, String key, TreeViewer viewer)
      Saves the selection and expanded states of the properties in the memento.
    • saveViewer

      public static void saveViewer(IFile file, TreeViewer viewer)
      Saves the selection and expanded states of the properties in the file permanent properties.
    • restoreTree

      public static void restoreTree(IFile file, IMemento memento, String key, TreeViewer viewer, PropCnr editedProperty)
      Restores the selection of the properties in the memento or the file, in the memento as a priority over the file.
      Parameters:
      file - The file to load properties from, or null for none.
      memento - The memento to use in priority over the file, null for none.
      key - The memento key to use.
      viewer - The tree viewer.
      editedProperty - The edited property.
    • saveViewer

      public static void saveViewer(TreeViewer viewer, StringBuilder selection, StringBuilder expanded)
      Saves the selection and expanded state of a viewer into two buffers.
    • viewerContainingAllProperties

      public static StructuredViewer viewerContainingAllProperties(Object viewer, GProp<?> single, GProp<?>[] props)
      Checks if a viewer container the properties in question. This method is used in the undo/redo operations for selecting in the viewers.
    • getPropsValue

      public static Object getPropsValue(PropCnr[] containers, Atom atom)
      Helper method to get a single value from many containers.
      Parameters:
      containers - The containers to look-up a value in.
      atom - The name of the property to get.
      Returns:
      MANY_VALUES if the value is not common in the containers, null for no value, otherwise the value.
    • getUniqueNames

      public static Atom[] getUniqueNames(PropCnr cnr, GProp<?>[] props, String suffix, boolean isCopy)
      Gets the names of properties to move to or add in a container. Support also exists if the component is already in the container, then the name is not changed.
      Parameters:
      cnr - The destination container.
      props - The properties.
      suffix - String to add when name exists, e.g. " Copy" or " Move".
    • getModuleModel

      public static ModuleModel getModuleModel(GProp<?> gp)
      Gets the IIZI Module Model from a property.
      Parameters:
      gp - The property (or null for none).
      Returns:
      The Module Model instance, or null if not found.
    • getModuleModel

      public static ModuleModel getModuleModel(GProp<?>... props)
      Gets the common Module Model from properties.