AbstractConfigurationTree.java from PovClipse at Krugle
Show AbstractConfigurationTree.java syntax highlighted
/*
* PovClipse - Eclipse plugin for editing and rendering Povray sceene files.
* Copyright (C) 2006-2007 Wolfgang Moestl wmoestl@web.de
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package com.wm.povclipse.actionsets.render.settings.tree;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PlatformUI;
import com.wm.povclipse.actionsets.render.RenderConfiguration;
import com.wm.povclipse.actionsets.render.RenderConfigurationException;
import com.wm.povclipse.actionsets.render.settings.IActionCmd;
import com.wm.povclipse.i18n.I18nRenderAS;
/**
* Abstract implementation of an tree interface for managing render
* configurations or render configuration templates.
*
* @author Wolfgang Möstl
*/
public abstract class AbstractConfigurationTree implements IConfigurationTree {
public static final String MENU_ITEM_DATA_TREE = "tree";
private Composite parent;
private Tree tree;
private SelectionListener listener;
protected TreeItem rootItem;
private MenuItem popupItemNew;
private MenuItem popupItemDelete;
private MenuItem popupItemDuplicate;
private Color colInvalid;
private Color colValid;
/**
* Constructor
* @param parent The parent <code>Composite</code>.
* @param style The widget style bits.
* @param listener The lestner to receive the tree selection eventes.
*/
public AbstractConfigurationTree (Composite parent, int style, SelectionListener listener) {
this.parent = parent;
this.listener = listener;
init(style, this.listener);
}
/**
* Reads all render configurations and adds them to the tree's root item.
*/
protected abstract void addAllRenderConfigs();
/**
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#isTemplateTree()
*/
public abstract boolean isTemplateTree();
/**
* Called to set an image icon to the <code>TreeItem</code>.
* @param treeItem
*/
protected abstract void setTreeItemIcon(TreeItem treeItem);
/**
* Creates the <code>Tree</code> object, populates it and expands the full tree.
* @param style The style bit for the <code>Tree</code> object.
* @param listener The <code>SelectionListener</code> to be aused by the
* <code>Tree</code>.
*/
private void init(int style, SelectionListener listener) {
colInvalid = new Color(parent.getDisplay(), 200, 0, 0);
colValid = new Color(parent.getDisplay(), 0, 0, 0);
tree = new Tree(parent, style);
tree.addSelectionListener(listener);
tree.setMenu(generatePopupMenu(parent.getShell(), listener));
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
tree.setData(MENU_ITEM_DATA_TREE, this);
// TreeColumn column = new TreeColumn(tree, SWT.LEFT);
// column.setWidth(200);
// tree.setSortColumn(column);
// tree.setSortDirection(SWT.DOWN);
rootItem = new TreeItem(tree, SWT.NONE);
addAllRenderConfigs();
// auto-expand the tree
expandFullTree();
}
/**
* Generates the common popup menu items:
* <ul>
* <li>New</li>
* <li>Duplicate</li>
* <li>Delete</li>
* </ul>
* @param shell The shell to be used for the produced <code>Menu</code> object.
* @param listener The <code>SelectionListener</code> to be used by the menu items.
* @return The produced <code>Menu</code> holding the <code>MenuItem</code> object mentioned above.
*/
protected Menu generatePopupMenu(Shell shell, SelectionListener listener) {
Menu popupMenu = new Menu(shell, SWT.POP_UP);
popupItemNew = new MenuItem(popupMenu, SWT.PUSH);
popupItemNew.setData(IActionCmd.COMMAND, IActionCmd.CMD_NEW);
popupItemNew.setData("ID", new Integer(1));
popupItemNew.setData(MENU_ITEM_DATA_TREE, this);
popupItemNew.addSelectionListener(listener);
popupItemNew.setText(I18nRenderAS.tree_popup_new);
popupItemNew.setImage(new Image(shell.getDisplay(), getClass().getClassLoader().
getResourceAsStream("icons/new_config.gif")));
popupItemDuplicate = new MenuItem(popupMenu, SWT.PUSH);
popupItemDuplicate.setData(IActionCmd.COMMAND, IActionCmd.CMD_DUPLICATE);
popupItemDuplicate.setData(MENU_ITEM_DATA_TREE, this);
popupItemDuplicate.addSelectionListener(listener);
popupItemDuplicate.setText(I18nRenderAS.tree_popup_duplicate);
popupItemDuplicate.setEnabled(hasSelection());
popupItemDuplicate.setImage(new Image(shell.getDisplay(), getClass().getClassLoader().
getResourceAsStream("icons/copy_config.gif")));
popupItemDelete = new MenuItem(popupMenu, SWT.PUSH);
popupItemDelete.addSelectionListener(listener);
popupItemDelete.setData(IActionCmd.COMMAND, IActionCmd.CMD_DELETE);
popupItemDelete.setData(MENU_ITEM_DATA_TREE, this);
popupItemDelete.setText(I18nRenderAS.tree_popup_delete);
popupItemDelete.setEnabled(hasSelection());
popupItemDelete.setImage(new Image(shell.getDisplay(), getClass().getClassLoader().
getResourceAsStream("icons/delete_config.gif")));
return popupMenu;
}
/**
* Reloads the full tree content.
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#reload()
*/
public void reload() {
rootItem.removeAll();
addAllRenderConfigs();
}
/**
* @return <code>true</code> if the tree has a selection,
* <code>false</code> otherwise.
*/
public boolean hasSelection() {
TreeItem[] selection = tree.getSelection();
if (selection.length == 0) return false;
if (selection[0].equals(rootItem))
return false;
else
return true;
}
/**
* Gets the parent <code>TreeItem</code> of the one displaying the given
* <code>RenderConfiguration</code>.
* @param renderConfig The <code>RenderConfiguration</code> to be searched for.
* @return See above.
*/
private TreeItem getParentTreeItem(RenderConfiguration renderConfig) {
// templates do not have projects, so return the rootItem
if (null == renderConfig.getProject())
return rootItem;
// return the existing project item or create a new one
TreeItem[] projectItems = rootItem.getItems();
for (int i=0; i<projectItems.length; i++) {
if (projectItems[i].getData() instanceof String) {
if (projectItems[i].getData().equals(renderConfig.getProject().getName()))
return projectItems[i];
}
}
// the project item does not exist yet, so create one
TreeItem item = new TreeItem(rootItem, SWT.NONE);
item.setData(renderConfig.getProject().getName());
item.setText(renderConfig.getProject().getName());
try {
item.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(org.eclipse.ui.ide.IDE.SharedImages.IMG_OBJ_PROJECT));
} catch (RuntimeException e) {
// looks like the IDE part is not installed
// => fall back to the standard folder image
item.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(org.eclipse.ui.ISharedImages.IMG_OBJ_FOLDER));
}
return item;
}
/**
* Creates a new <code>TreeItem</code> displaying the given
* <code>RenderConfiguration</code> and returns it.
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#addItem(com.wm.povclipse.actionsets.render.RenderConfiguration)
*/
public TreeItem addItem(RenderConfiguration renderConfig) {
TreeItem parentItem = getParentTreeItem(renderConfig);
TreeItem item = new TreeItem(parentItem, SWT.NONE);
item.setData(renderConfig);
item.setText(renderConfig.getName());
setTreeItemIcon(item);
try {
renderConfig.checkConfig();
} catch (RenderConfigurationException e) {
// do nothing with the RenderConfigurationException
}
item.setForeground(renderConfig.isValid() ? colValid : colInvalid);
return item;
}
/**
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#setPopupMenuConfigEnabled(boolean)
*/
public void setPopupMenuConfigEnabled(boolean enabled) {
popupItemDelete.setEnabled(enabled);
popupItemDuplicate.setEnabled(enabled);
}
/**
* Selects a specific <code>TreeItem</code>.
* @param item the <code>TreeItem</code> to be selected.
*/
private void selectItem(TreeItem item) {
tree.setSelection(item);
}
/**
* Gets the selected item.
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#getSelectedItem()
* @return The selected <code>TreeItem</code> or <code>NULL</code>
* if no item is selected.
*/
public TreeItem getSelectedItem() {
TreeItem[] selection = tree.getSelection();
if (selection.length > 0)
return selection[0];
else
return null;
}
/**
* Gets the <code>TreeItem</code> displaying the given <code>RenderConfiguration</code>.
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#getItemByRenderConfiguration(com.wm.povclipse.actionsets.render.RenderConfiguration)
*/
public TreeItem getItemByRenderConfiguration(RenderConfiguration renderConfig) {
return getItemByRenderConfiguration(rootItem, renderConfig);
}
/**
* Gets the <code>TreeItem</code> displaying the given <code>RenderConfiguration</code>,
* starting the search with <code>item</code>.
* @param item The <code>TreeItem</code> so start the search with.
* @param renderConfig The <code>RenderConfiguration</code> to be searched.
* @return The <code>TreeItem</code> displaying the given <code>RenderConfiguration</code>
* or <code>null</code> if non can be found.
*/
private TreeItem getItemByRenderConfiguration(TreeItem item, RenderConfiguration renderConfig) {
if (renderConfig.equals(item.getData()))
return item;
TreeItem[] items = item.getItems();
for (int i=0; i<items.length; i++) {
TreeItem ret = getItemByRenderConfiguration(items[i], renderConfig);
if (null != ret)
return ret;
}
return null;
}
/**
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#selectItem(com.wm.povclipse.actionsets.render.RenderConfiguration)
*/
public void selectItem(RenderConfiguration renderConfig) {
TreeItem item = getItemByRenderConfiguration(renderConfig);
if (null != item) {
selectItem(item);
setPopupMenuConfigEnabled(item.getParentItem() != null);
}
}
/**
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#removeItem(org.eclipse.swt.widgets.TreeItem)
*/
public void removeItem(TreeItem item) {
rootItem.removeAll();
addAllRenderConfigs();
}
/**
* @see com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree#setItemColor(org.eclipse.swt.widgets.TreeItem, boolean)
*/
public void setItemColor(TreeItem item, boolean isValid) {
if (null != item) {
item.setForeground(isValid ? colValid : colInvalid);
}
}
/**
* Expands the full tree down to the leaves.
*/
public void expandFullTree() {
expandAllNodes(rootItem);
}
/**
* Expands the <code>item</code> sub-tree down to the leaves.
* @param item The <code>TreeItems</code> to expand.
*/
private void expandAllNodes(TreeItem item) {
item.setExpanded(true);
TreeItem[] childs = item.getItems();
for (int i=0; i<childs.length; i++)
expandAllNodes(childs[i]);
}
}
See more files for this project here