/** * Copyright 2009 Red Hat, Inc. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.safehaus.penrose.studio.properties.editor; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.*; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.forms.widgets.*; import org.safehaus.penrose.studio.parameter.ParameterDialog; import org.safehaus.penrose.studio.dialog.ErrorDialog; import org.apache.log4j.Logger; import java.util.Iterator; import java.util.Map; /** * @author Endi S. Dewata */ public class SystemPropertiesPage { Logger log = Logger.getLogger(getClass()); FormToolkit toolkit; Table parametersTable; Button addButton; Button editButton; Button removeButton; SystemPropertiesEditor editor; Map properties; public SystemPropertiesPage(SystemPropertiesEditor editor) { this.editor = editor; properties = editor.properties; } public Control createControl() { toolkit = new FormToolkit(editor.getParent().getDisplay()); Form form = toolkit.createForm(editor.getParent()); form.setText("System Properties"); Composite body = form.getBody(); body.setLayout(new GridLayout()); Section section = toolkit.createSection(body, Section.TITLE_BAR | Section.EXPANDED); section.setText("Properties"); section.setLayoutData(new GridData(GridData.FILL_BOTH)); Control parametersSection = createPropertiesSection(section); section.setClient(parametersSection); return form; } public Composite createPropertiesSection(final Composite parent) { Composite composite = toolkit.createComposite(parent); composite.setLayout(new GridLayout(2, false)); parametersTable = toolkit.createTable(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI); parametersTable.setHeaderVisible(true); parametersTable.setLinesVisible(true); parametersTable.setLayoutData(new GridData(GridData.FILL_BOTH)); parametersTable.addMouseListener(new MouseAdapter() { public void mouseDoubleClick(MouseEvent event) { try { if (parametersTable.getSelectionCount() == 0) return; int index = parametersTable.getSelectionIndex(); TableItem item = parametersTable.getSelection()[0]; String oldName = item.getText(0); String oldValue = item.getText(1); ParameterDialog dialog = new ParameterDialog(parent.getShell(), SWT.NONE); dialog.setText("Edit parameter..."); dialog.setName(oldName); dialog.setValue(oldValue); dialog.open(); if (dialog.getAction() == ParameterDialog.CANCEL) return; String newName = dialog.getName(); String newValue = dialog.getValue(); if (!oldName.equals(newName)) { properties.remove(oldName); } properties.put(newName, newValue); refresh(); parametersTable.setSelection(index); checkDirty(); } catch (Exception e) { log.error(e.getMessage(), e); ErrorDialog.open(e); } } }); TableColumn tc = new TableColumn(parametersTable, SWT.NONE); tc.setText("Name"); tc.setWidth(250); tc = new TableColumn(parametersTable, SWT.NONE); tc.setText("Value"); tc.setWidth(250); Composite buttons = toolkit.createComposite(composite); buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL)); buttons.setLayout(new GridLayout()); addButton = new Button(buttons, SWT.PUSH); addButton.setText("Add"); addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); addButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { ParameterDialog dialog = new ParameterDialog(parent.getShell(), SWT.NONE); dialog.setText("Add parameter..."); dialog.open(); if (dialog.getAction() == ParameterDialog.CANCEL) return; properties.put(dialog.getName(), dialog.getValue()); refresh(); checkDirty(); } catch (Exception e) { log.error(e.getMessage(), e); ErrorDialog.open(e); } } }); editButton = new Button(buttons, SWT.PUSH); editButton.setText("Edit"); editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); editButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { if (parametersTable.getSelectionCount() == 0) return; int index = parametersTable.getSelectionIndex(); TableItem item = parametersTable.getSelection()[0]; String oldName = item.getText(0); String oldValue = item.getText(1); ParameterDialog dialog = new ParameterDialog(parent.getShell(), SWT.NONE); dialog.setText("Edit parameter..."); dialog.setName(oldName); dialog.setValue(oldValue); dialog.open(); if (dialog.getAction() == ParameterDialog.CANCEL) return; String newName = dialog.getName(); String newValue = dialog.getValue(); if (!oldName.equals(newName)) { properties.remove(oldName); } properties.put(newName, newValue); refresh(); parametersTable.setSelection(index); checkDirty(); } catch (Exception e) { log.error(e.getMessage(), e); ErrorDialog.open(e); } } }); removeButton = new Button(buttons, SWT.PUSH); removeButton.setText("Remove"); removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); removeButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { if (parametersTable.getSelectionCount() == 0) return; TableItem items[] = parametersTable.getSelection(); for (int i=0; i<items.length; i++) { String name = items[i].getText(0); properties.remove(name); } refresh(); checkDirty(); } catch (Exception e) { log.error(e.getMessage(), e); ErrorDialog.open(e); } } }); refresh(); return composite; } public void refresh() { parametersTable.removeAll(); for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) { String name = (String)i.next(); String value = (String)properties.get(name); TableItem item = new TableItem(parametersTable, SWT.CHECK); item.setText(0, name); item.setText(1, value); } } public void load() { refresh(); } public void checkDirty() { editor.checkDirty(); } public void dispose() { toolkit.dispose(); } }