RenderSettingsDialog.java from PovClipse at Krugle
Show RenderSettingsDialog.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;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import com.wm.povclipse.LoggerProvider;
import com.wm.povclipse.PovClipseEditorPlugin;
import com.wm.povclipse.IErrorDisplayer;
import com.wm.povclipse.actionsets.render.RenderActionSetPulldown;
import com.wm.povclipse.actionsets.render.RenderConfiguration;
import com.wm.povclipse.actionsets.render.RenderConfigurationException;
import com.wm.povclipse.actionsets.render.RenderConfigurationStore;
import com.wm.povclipse.actionsets.render.settings.tree.ConfigurationTemplateTree;
import com.wm.povclipse.actionsets.render.settings.tree.ConfigurationTree;
import com.wm.povclipse.actionsets.render.settings.tree.IConfigurationTree;
import com.wm.povclipse.i18n.I18nCommon;
import com.wm.povclipse.i18n.I18nError;
import com.wm.povclipse.i18n.I18nRenderAS;
import com.wm.povclipse.utils.MessageUtil;
import com.wm.povclipse.utils.StringUtils;
/**
* @author Wolfgang Möstl
*/
public class RenderSettingsDialog extends Dialog implements SelectionListener, KeyListener, IErrorDisplayer
{
static private Logger logger = LoggerProvider.getLogger(RenderSettingsDialog.class.getName());
protected static final String KEY = "KEY";
protected static final String KEY_NAME = "NAME";
private Label labError;
private IConfigurationTree selectionTree;
private IConfigurationTree templateTree;
private RenderSettings settingsComposite;
private SashForm mainSash;
private SashForm treeSash;
private IPath filePath;
private RenderConfiguration renderConfig;
private Button btnOpen;
private Button btnCancel;
private TreeItem selectedItem;
private RenderActionSetPulldown pullDownProvider = null;
private static final int BTN_OK = 8999;
private static final int BTN_CANCEL = 8998;
/**
* Creates a tray dialog instance. Note that the window will have no visual
* representation (no widgets) until it is told to open.
*
* @param shell the parent shell, or <code>null</code> to create a top-level shell
* @param filePath the path of the selected file, relative to it's project.
*/
public RenderSettingsDialog(Shell shell, IPath filePath) {
super(shell);
this.filePath = filePath;
}
/**
* Creates a tray dialog instance. Note that the window will have no visual
* representation (no widgets) until it is told to open.
*
* @param shell the parent shell, or <code>null</code> to create a top-level shell
* @param filePath the path of the selected file, relative to it's project.
*/
public RenderSettingsDialog(Shell shell, RenderConfiguration renderConfig) {
super(shell);
this.renderConfig = renderConfig;
}
/**
* Sets the dialog title and image.
* @see org.eclipse.jface.dialogs.Dialog#create()
*/
public void create() {
super.create();
getShell().setText(I18nRenderAS.dialog_title);
getShell().setImage(PovClipseEditorPlugin.getDefault().getImageRegistry().get(PovClipseEditorPlugin.IMAGE_LOGO_POVRAY));
setDefaultImage(PovClipseEditorPlugin.getDefault().getImageRegistry().get(PovClipseEditorPlugin.IMAGE_LOGO_POVRAY));
}
/**
* Creates the dialog content.
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
createTitleBar(composite);
mainSash = new SashForm(composite, SWT.HORIZONTAL | SWT.SMOOTH);
mainSash.setLayout(new FillLayout());
mainSash.setEnabled(true); // enable resize
mainSash.setLayoutData(new GridData(GridData.FILL_BOTH));
treeSash = new SashForm(mainSash, SWT.VERTICAL | SWT.SMOOTH);
treeSash.setLayout(new FillLayout());
treeSash.setEnabled(true); // enable resize
treeSash.setLayoutData(new GridData(GridData.FILL_BOTH));
selectionTree = new ConfigurationTree (treeSash, SWT.SINGLE | SWT.BORDER, this);
templateTree = new ConfigurationTemplateTree(treeSash, SWT.SINGLE | SWT.BORDER, this);
settingsComposite = new RenderSettings(mainSash, SWT.BORDER, this, this, this);
if (filePath != null) {
settingsComposite.setFileInfo(filePath);
} else if (renderConfig != null) {
settingsComposite.setRenderConfiguration(renderConfig);
}
mainSash.setWeights(new int[] {25, 75} );
treeSash.setWeights(new int[] {75, 25} );
// display the context-menu hint
Label labPopup = new Label(composite, SWT.LEFT);
labPopup.setText(I18nRenderAS.hint_context);
return composite;
}
/**
* Creates the title bar.
* @param parent The parent to receive the bar control.
* @return The bar control.
*/
protected Control createTitleBar(Composite parent) {
Composite outerComposite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = false;
// layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
// layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
// layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
// layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
outerComposite.setLayout(layout);
GridData data = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, true, false);
outerComposite.setLayoutData(data);
outerComposite.setFont(parent.getFont());
// create the composite for the title text
Composite textComposite = new Composite(outerComposite, SWT.NONE);
// Add the gears image
Label labImage = new Label(outerComposite, SWT.LEFT);
labImage.setImage(PovClipseEditorPlugin.getDefault().getImageRegistry().get(PovClipseEditorPlugin.IMAGE_GEARS));
labImage.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
// create a layout with spacing and margins appropriate for the font
// size.
layout = new GridLayout();
layout.numColumns = 1;
layout.makeColumnsEqualWidth = true;
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
textComposite.setLayout(layout);
data = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, true, false);
textComposite.setLayoutData(data);
textComposite.setFont(parent.getFont());
// Add the buttons to the button bar.
// Add the title
Label titleLabel = new Label(textComposite, SWT.CENTER);
titleLabel.setFont(JFaceResources.getHeaderFont());
titleLabel.setText(I18nRenderAS.settings_title);
titleLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
labError = new Label(textComposite, SWT.CENTER);
labError.setFont(JFaceResources.getDefaultFont());
labError.setForeground(new Color(this.getShell().getDisplay(), 200, 0, 0));
labError.setText("");
labError.setLayoutData(new GridData(GridData.FILL_BOTH));
return outerComposite;
}
/**
* Clears the error text.
*/
public void clearErrorText() {
labError.setText("");
}
/**
* Sets the error message using the given <code>Throwable</code>.
* @param throwable The <code>Throwable</code> Error object who's message is to be displayed.
*/
public void setErrorText(Throwable throwable) {
String msg = throwable.getMessage().trim();
msg = msg.replace('\r', ' ').replace('\n', ' ');
labError.setText(msg);
}
/**
* @return <code>true</code> if the render configuration was saved successfull,
* <code>false</code> otherwise.
*/
protected boolean okClicked() {
// save the config
boolean configOK = settingsComposite.doApplyButton();
if (configOK) {
try {
// fetch the render configuration
renderConfig = settingsComposite.getRenderConfiguration(true);
super.okPressed();
// do not try to render templates!
if (!renderConfig.isTemplate()) {
RenderActionSetPulldown.render(renderConfig, getShell());
}
} catch (RenderConfigurationException e) {
MessageDialog.openError(Display.getCurrent().getActiveShell(), I18nRenderAS.msg_err_title,
I18nRenderAS.bind(I18nRenderAS.msg_err_config_load, new Object[] {settingsComposite.getName(), e.getMessage()}));
}
}
else {
if (labError.getText() != null && labError.getText().length() > 0) {
MessageDialog.openError(Display.getCurrent().getActiveShell(), I18nRenderAS.msg_err_title,
labError.getText());
}
}
return configOK;
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent e) {
// noting to do
}
/**
* Enables or disables the "OK" button and displays or hides the
* render configuration details.
* @param enabled <code>true</code> to enable the "OK" button and
* display thr render configuration details, <code>false</code> to disable the "OK"
* button and hide the render configuration details.
*/
private void enableRenderConfigurationDetails(boolean enabled) {
btnOpen.setEnabled(enabled);
settingsComposite.setEnabled(enabled);
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
*/
protected void createButtonsForButtonBar(Composite parent) {
btnOpen = createButton(parent, BTN_OK, I18nCommon.common_button_ok, true);
btnOpen.setEnabled(false);
btnOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setReturnCode(BTN_OK);
if (okClicked()) {
// close only on success
close();
}
}
});
btnCancel = createButton(parent, BTN_CANCEL, I18nCommon.common_button_cancel, false);
btnCancel.setEnabled(true);
btnCancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setReturnCode(BTN_CANCEL);
close();
}
});
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent e) {
if (e.getSource() instanceof MenuItem) {
MenuItem menuItem = (MenuItem)e.getSource();
String cmd = (String)menuItem.getData(IActionCmd.COMMAND);
logger.info("Selected: " + menuItem.getText() + ", CMD=" + cmd);
IConfigurationTree tree = (IConfigurationTree)menuItem.getData(ConfigurationTree.MENU_ITEM_DATA_TREE);
if (IActionCmd.CMD_NEW.equals(cmd)) {
renderConfig = new RenderConfiguration();
renderConfig.setTemplate(tree.isTemplateTree());
settingsComposite.setRenderConfiguration(renderConfig);
selectedItem = tree.addItem(renderConfig);
tree.selectItem(renderConfig);
enableRenderConfigurationDetails(true);
} else if (IActionCmd.CMD_DUPLICATE.equals(cmd)) {
RenderConfiguration renderConfig = getSelectedRenderConfiguration(tree);
try {
RenderConfiguration newConfig = renderConfig.createClone();
newConfig.setName(newConfig.getName() + "-CLONE");
newConfig.clearLoadedFileName();
newConfig.createStorageFileName();
settingsComposite.setRenderConfiguration(newConfig);
selectedItem = tree.addItem(newConfig);
tree.selectItem(newConfig);
enableRenderConfigurationDetails(true);
} catch (CloneNotSupportedException cnsEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, cnsEx.getLocalizedMessage());
} catch (RenderConfigurationException rcEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, rcEx.getLocalizedMessage());
}
} else if (IActionCmd.CMD_DELETE.equals(cmd)) {
RenderConfiguration renderConfig = getSelectedRenderConfiguration(tree);
try {
// if we have deleted the render config beeing stored as
// the one beeing used the last time we have
// to clear this information!
RenderConfiguration lastRenderConfig = pullDownProvider.getLastRunFromPropertyStore();
if (null != lastRenderConfig && lastRenderConfig.equals(renderConfig))
pullDownProvider.clearLastRunConfig();
renderConfig.delete();
tree.removeItem(getSelectedTreeItem(tree));
selectedItem.dispose();
selectedItem = null;
} catch (IOException ioEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, ioEx.getLocalizedMessage());
}
} else if (IActionCmd.CMD_SAVE_AS_TEMPLATE.equals(cmd)) {
RenderConfiguration renderConfig = getSelectedRenderConfiguration(tree);
createTemplateClone(renderConfig);
} else if (IActionCmd.CMD_NEW_FROM_TEMPLATE.equals(cmd)) {
RenderConfiguration renderConfig = getSelectedRenderConfiguration(tree);
RenderConfiguration newConfig = null;
// get the render configuration name from the user
String errorMsg = null;
boolean isOK = false;
String newName = renderConfig.getName() + "-CLONE";
while (!isOK) {
// ask for the name to be used
InputDialog nameDialog = new InputDialog(e.display.getActiveShell(),
I18nRenderAS.config_name_input_dialog_title,
I18nRenderAS.config_name_input_dialog_message,
newName,
null);
int ret = nameDialog.open();
if (ret == Dialog.OK) {
newName = nameDialog.getValue();
try {
newConfig = renderConfig.createClone();
newConfig.setName(newName);
newConfig.setTemplate(false);
newConfig.clearLoadedFileName();
errorMsg = null;
} catch (CloneNotSupportedException cnsEx) {
errorMsg = cnsEx.getMessage();
} finally {
// exit the loop
isOK = true;
}
} else {
// exit the loop
isOK = true;
}
}
if (errorMsg != null && errorMsg.length() > 0) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, errorMsg);
} else {
settingsComposite.setRenderConfiguration(newConfig);
selectedItem = selectionTree.addItem(newConfig);
selectionTree.selectItem(newConfig);
enableRenderConfigurationDetails(true);
}
}
else {
// unkonw command!
logger.warn("WARNING: unknown command: " + cmd);
}
}
else if (e.getSource() instanceof Tree) {
TreeItem item = ((Tree)e.getSource()).getSelection()[0];
IConfigurationTree tree = (IConfigurationTree)((Tree)e.getSource()).getData(ConfigurationTree.MENU_ITEM_DATA_TREE);
logger.debug("Selected: '" + item.getText() + "' in tree " + StringUtils.getPartAfterLastCharacter(tree.getClass().getName(), '.'));
// enable or disable the Delete and Duplicate popup menu items
if (tree != null)
tree.setPopupMenuConfigEnabled(item.getParentItem() != null);
if (null == item.getParentItem()) {
// Root item, disable Render Config
renderConfig = new RenderConfiguration();
settingsComposite.setRenderConfiguration(renderConfig);
enableRenderConfigurationDetails(false);
} else {
// Render config!
Object obj = item.getData();
if (obj instanceof RenderConfiguration) {
selectedItem = ((Tree)e.getSource()).getSelection()[0];
if (null != selectedItem) {
selectedItem.setText(((RenderConfiguration)selectedItem.getData()).getName());
}
renderConfig = (RenderConfiguration)obj;
settingsComposite.setRenderConfiguration(renderConfig);
}
enableRenderConfigurationDetails(true);
}
}
else
logger.info("Selected: " + e.getSource().getClass().getName());
}
protected void createTemplateClone(RenderConfiguration config) {
try {
RenderConfiguration templateConfig = saveRenderConfigurationAsTemplate(config, this.getShell());
settingsComposite.setRenderConfiguration(templateConfig);
selectedItem = templateTree.addItem(templateConfig);
templateTree.selectItem(templateConfig);
enableRenderConfigurationDetails(true);
} catch (RenderConfigurationException rcEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, rcEx.getLocalizedMessage());
} catch (CloneNotSupportedException cnsEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, cnsEx.getLocalizedMessage());
} catch (IOException ioEx) {
MessageDialog.openError(getShell(), I18nError.err_dialog_title, ioEx.getLocalizedMessage());
}
}
private RenderConfiguration saveRenderConfigurationAsTemplate(RenderConfiguration config, Shell shell)
throws RenderConfigurationException, CloneNotSupportedException, IOException {
RenderConfiguration templateConfig = null;
boolean isOK = false;
String newName = config.getName() + " " + I18nCommon.common_template;
while (!isOK) {
// ask for the name to be used
InputDialog nameDialog = new InputDialog(shell,
I18nRenderAS.template_name_input_dialog_title,
I18nRenderAS.template_name_input_dialog_message,
newName,
null);
int ret = nameDialog.open();
if (ret == Dialog.OK) {
newName = nameDialog.getValue();
File f = new File(RenderConfigurationStore.getTemplateFolder(), newName + RenderConfiguration.FILE_NAME_ENDING);
boolean write = true;
if (f.exists()) {
write = MessageUtil.showQuestionMessage(
shell,
I18nRenderAS.template_question_overwrite_title,
I18nRenderAS.bind(I18nRenderAS.template_question_overwrite_message, newName));
}
if (write) {
// the file either does not exist
// or the user has decided to overwrite it
// => clone the config as template and save it
try {
templateConfig = config.createClone();
templateConfig.setTemplate(true);
templateConfig.clearLoadedFileName();
templateConfig.setName(newName);
templateConfig.checkConfig();
if (!templateConfig.save())
templateConfig = null;
} finally {
// exit the loop
isOK = true;
}
}
} else {
// exit the loop
isOK = true;
}
}
return templateConfig;
}
private TreeItem getSelectedTreeItem(IConfigurationTree tree) {
return tree.getSelectedItem();
}
private TreeItem getSelectedTemplateTreeItem() {
return templateTree.getSelectedItem();
}
private RenderConfiguration getSelectedRenderConfiguration(IConfigurationTree tree) {
TreeItem item = getSelectedTreeItem(tree);
if (null == item)
return null;
else
return (RenderConfiguration)item.getData();
}
public void keyPressed(KeyEvent e) {
// do noting
}
public void keyReleased(KeyEvent e) {
if (e.getSource() instanceof Text) {
if (KEY_NAME.equals(((Text)e.getSource()).getData(KEY))) {
selectedItem.setText(((Text)e.getSource()).getText());
clearErrorText();
}
}
}
public void setPullDownProvider(RenderActionSetPulldown pullDownProvider) {
this.pullDownProvider = pullDownProvider;
}
public void setSelectionTreeItemColor(RenderConfiguration config) {
if (null != config)
selectionTree.setItemColor(getSelectedTreeItem(selectionTree), config.isValid());
}
public void setTemplateTreeItemColor(RenderConfiguration config) {
if (null != config)
templateTree.setItemColor(getSelectedTemplateTreeItem(), config.isValid());
}
public boolean selectionTreeContainsRenderConfig(RenderConfiguration config) {
return null != selectionTree.getItemByRenderConfiguration(config);
}
public boolean templateTreeTreeContainsRenderConfig(RenderConfiguration config) {
return null != templateTree.getItemByRenderConfiguration(config);
}
public void reloadTemplateTree(boolean reselectSelection) {
RenderConfiguration selectedConfig = null;
if (reselectSelection) {
TreeItem selectedConfigItem = templateTree.getSelectedItem();
if (null != selectedConfigItem)
selectedConfig = (RenderConfiguration)selectedConfigItem.getData();
}
templateTree.reload();
if (reselectSelection && null != selectedConfig) {
templateTree.selectItem(selectedConfig);
}
}
public void reloadSelectionTree(boolean reselectSelection) {
RenderConfiguration selectedConfig = null;
if (reselectSelection) {
TreeItem selectedConfigItem = selectionTree.getSelectedItem();
if (null != selectedConfigItem)
selectedConfig = (RenderConfiguration)selectedConfigItem.getData();
}
selectionTree.reload();
if (reselectSelection && null != selectedConfig) {
selectionTree.selectItem(selectedConfig);
}
}
}
See more files for this project here