package org.eclipse.maven.ui.preferences;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jface.preference.*;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.maven.Activator;
import org.eclipse.maven.core.MavenCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;
public class MavenCommandsPreferencePage extends PreferencePage implements
IWorkbenchPreferencePage {
private Table commandstable;
private Map<String, MavenCommand> commands;
private Set<String> selectedCommandsId;
private Map<String, TableItem> tableItems;
private IEclipsePreferences nodeStore;
private int maxCommandId = 0;
private int defaultColumnWidth = 150;
private int defaultTableHeight = 200;
public MavenCommandsPreferencePage() {
super();
commands = new HashMap<String, MavenCommand>();
selectedCommandsId = new HashSet<String>();
tableItems = new HashMap<String, TableItem>();
}
public void init(IWorkbench iworkbench) {
nodeStore = new InstanceScope().getNode(Activator.PLUGIN_ID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
try {
loadPreferences();
} catch (BackingStoreException e) {
Activator.logError("", e);
StatusManager
.getManager()
.handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"There was an error while loading the commands preferences.", e),
StatusManager.SHOW);
}
setDescription("Add, edit or remove custom maven commands...");
}
protected Control createContents(Composite parent) {
Composite content = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
content.setLayout(layout);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
content.setLayoutData(gridData);
noDefaultAndApplyButton();
createTable(content);
fillTable();
createButtonGroup(content);
return content;
}
public Table createTable(Composite parent) {
commandstable = new Table(parent, SWT.MULTI | SWT.BORDER);
commandstable.setLayoutData(new GridData(GridData.FILL_BOTH));
TableLayout tableLayout = new TableLayout();
commandstable.setLayout(tableLayout);
commandstable.setHeaderVisible(true);
commandstable.setLinesVisible(true);
TableColumn column = new TableColumn(commandstable, SWT.CENTER
| SWT.BORDER_SOLID, 0);
column.setText("Alias");
ColumnWeightData columnLayout = new ColumnWeightData(200);
tableLayout.addColumnData(columnLayout);
column = new TableColumn(commandstable, SWT.CENTER | SWT.BORDER_SOLID,
1);
column.setText("Active");
columnLayout = new ColumnWeightData(50);
tableLayout.addColumnData(columnLayout);
return commandstable;
}
private void fillTable() {
int i = 0;
for (Map.Entry<String, MavenCommand> entry : commands.entrySet()) {
TableItem item = new TableItem(commandstable, SWT.LEFT);
item.setText(entry.getValue().getAliasValue());
item.setData("id", entry.getKey());
item.setData("indexInTable", i);
tableItems.put(entry.getKey(), item);
i++;
}
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MeasureItem: {
TableItem item = (TableItem) event.item;
String text = item.getText(event.index);
Point size = event.gc.textExtent(text);
event.width = defaultColumnWidth;
event.height = Math.max(event.height, size.y);
break;
}
case SWT.PaintItem: {
TableItem item = (TableItem) event.item;
String text = item.getText(event.index);
Point size = event.gc.textExtent(text);
int offset2 = event.index == 0 ? Math.max(0,
(event.height - size.y) / 2) : 0;
event.gc.drawText(text, event.x, event.y + offset2, true);
break;
}
case SWT.EraseItem: {
event.detail &= ~SWT.FOREGROUND;
break;
}
case SWT.Selection: {
if (event.item != null) {
TableItem item = (TableItem) event.item;
Table table = item.getParent();
TableItem[] selection = table.getSelection();
selectedCommandsId.clear();
for (TableItem ti : selection)
selectedCommandsId.add((String) ti.getData("id"));
}
}
}
}
};
commandstable.addListener(SWT.Selection, paintListener);
commandstable.addListener(SWT.MeasureItem, paintListener);
commandstable.addListener(SWT.PaintItem, paintListener);
commandstable.addListener(SWT.EraseItem, paintListener);
for (TableColumn column : commandstable.getColumns())
column.pack();
}
private void createButtonGroup(Composite top) {
Composite buttonGroup = new Composite(top, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
buttonGroup.setLayout(layout);
buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL
| GridData.HORIZONTAL_ALIGN_FILL));
buttonGroup.setFont(top.getFont());
((GridData) buttonGroup.getLayoutData()).widthHint = 75;
addButtonsToButtonGroup(buttonGroup);
}
protected void addButtonsToButtonGroup(Composite parent) {
Button b = createPushButton2(parent, "Add");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
MavenCommand command = new MavenCommand();
command.setId("" + maxCommandId);
MavenCommandDialog dialog = new MavenCommandDialog(new Shell(),
command);
dialog.create();
int result = dialog.open();
if (result == 0) {
commands.put(command.getId(), command);
nodeStore.put(PreferencesConstants.MAVEN_COMMAND_ALIAS
+ "." + maxCommandId, command.getAliasValue());
nodeStore.put(PreferencesConstants.MAVEN_COMMANDLINE + "."
+ maxCommandId, command.getCommandLine());
TableItem item = new TableItem(commandstable, SWT.LEFT);
item.setText(command.getAliasValue());
item.setData("id", command.getId());
item.setData("indexInTable", tableItems.size());
tableItems.put(command.getId(), item);
maxCommandId++;
}
}
});
b = createPushButton2(parent, "Edit");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (selectedCommandsId.size() == 1) {
String id = selectedCommandsId.iterator().next();
MavenCommand command = commands.get(id);
MavenCommandDialog dialog = new MavenCommandDialog(
new Shell(), command);
dialog.create();
int result = dialog.open();
if (result == 0) {
;
commands.put(PreferencesConstants.MAVEN_COMMAND_ALIAS
+ "." + id, command);
nodeStore.put(PreferencesConstants.MAVEN_COMMAND_ALIAS
+ "." + id, command.getAliasValue());
nodeStore.put(PreferencesConstants.MAVEN_COMMANDLINE
+ "." + id, command.getCommandLine());
tableItems.get(id).setText(command.getAliasValue());
selectedCommandsId.clear();
maxCommandId++;
}
}
}
});
b = createPushButton2(parent, "Remove");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
for (String id : selectedCommandsId) {
tableItems.get(id).dispose();
tableItems.remove(id);
}
selectedCommandsId.clear();
}
});
}
private Button createPushButton2(Composite parent, String label) {
Button button = createPushButton(parent, label);
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
| GridData.FILL_HORIZONTAL);
button.setLayoutData(gridData);
return button;
}
private Button createPushButton(Composite parent, String label) {
Button button = new Button(parent, SWT.PUSH);
button.setText(label);
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
| GridData.FILL_HORIZONTAL);
button.setLayoutData(gridData);
setButtonLayoutData(button);
return button;
}
protected void loadPreferences() throws BackingStoreException {
int commandNumber = 0;
String[] keys = nodeStore.keys();
if (keys != null)
for (String key : keys) {
if (key.contains(PreferencesConstants.MAVEN_COMMAND_ALIAS)) {
String sId = key
.substring(PreferencesConstants.MAVEN_COMMAND_ALIAS
.length() + 1);
commandNumber = Integer.parseInt(sId);
if (maxCommandId <= commandNumber)
maxCommandId = commandNumber + 1;
MavenCommand command = new MavenCommand();
command.setId("" + commandNumber);
command.setAliasValue(nodeStore.get(
PreferencesConstants.MAVEN_COMMAND_ALIAS + "."
+ commandNumber, null));
command.setCommandLine(nodeStore.get(
PreferencesConstants.MAVEN_COMMANDLINE + "."
+ commandNumber, null));
commands.put(command.getId(), command);
}
}
}
@Override
public boolean performOk() {
try {
for (Map.Entry<String, MavenCommand> entry : commands.entrySet()) {
if (!tableItems.containsKey(entry.getKey())) {
nodeStore.remove(PreferencesConstants.MAVEN_COMMAND_ALIAS
+ "." + entry.getKey());
nodeStore.remove(PreferencesConstants.MAVEN_COMMANDLINE
+ "." + entry.getKey());
}
}
nodeStore.flush();
} catch (BackingStoreException e) {
Activator.logError("", e);
StatusManager
.getManager()
.handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"Error while saving the preferences.", e),
StatusManager.SHOW);
}
return true;
}
}