diff --git a/asciidoctor-editor-feature/feature.xml b/asciidoctor-editor-feature/feature.xml index f297820..9d08b5d 100644 --- a/asciidoctor-editor-feature/feature.xml +++ b/asciidoctor-editor-feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/asciidoctor-editor-plugin/META-INF/MANIFEST.MF b/asciidoctor-editor-plugin/META-INF/MANIFEST.MF index d72028c..a92d272 100644 --- a/asciidoctor-editor-plugin/META-INF/MANIFEST.MF +++ b/asciidoctor-editor-plugin/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Asciidoctor Editor Bundle-SymbolicName: de.jcup.asciidoctoreditor;singleton:=true -Bundle-Version: 2.4.1 +Bundle-Version: 2.5.0 Bundle-Activator: de.jcup.asciidoctoreditor.AsciiDoctorEditorActivator Bundle-Vendor: Albert Tregnaghi Require-Bundle: org.eclipse.ui, diff --git a/asciidoctor-editor-plugin/plugin.xml b/asciidoctor-editor-plugin/plugin.xml index 7568912..2dbfd0a 100644 --- a/asciidoctor-editor-plugin/plugin.xml +++ b/asciidoctor-editor-plugin/plugin.xml @@ -629,6 +629,15 @@ + + + + customEnvEntries = null; + if (CustomEnvironmentEntrySupport.DEFAULT.areCustomEnvironmentEntriesEnabled()) { + customEnvEntries = CustomEnvironmentEntrySupport.DEFAULT.fetchConfiguredEnvironmentEntriesAsMap(); + } + aspServerAdapter.setCustomEnvironmentEntries(customEnvEntries); + aspServerAdapter.startServer(); } diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/asciidoc/InstalledAsciidoctorAdapter.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/asciidoc/InstalledAsciidoctorAdapter.java index be3f5c8..0c409d1 100644 --- a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/asciidoc/InstalledAsciidoctorAdapter.java +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/asciidoc/InstalledAsciidoctorAdapter.java @@ -28,6 +28,7 @@ import de.jcup.asciidoctoreditor.OSUtil; import de.jcup.asciidoctoreditor.console.AsciiDoctorConsoleUtil; import de.jcup.asciidoctoreditor.preferences.AsciiDoctorEditorPreferences; +import de.jcup.asciidoctoreditor.preferences.CustomEnvironmentEntrySupport; import de.jcup.asciidoctoreditor.util.AsciiDoctorEditorUtil; import de.jcup.asp.client.AspClientProgressMonitor; @@ -47,6 +48,10 @@ public void convertFile(File editorFileOrNull, File asciiDocFile, Map commands) { StringBuilder commandLine = new StringBuilder(); for (String command : commands) { diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorEnvironmentPreferencePage.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorEnvironmentPreferencePage.java new file mode 100644 index 0000000..a0652e0 --- /dev/null +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/AsciiDoctorEditorEnvironmentPreferencePage.java @@ -0,0 +1,423 @@ +package de.jcup.asciidoctoreditor.preferences; + +import static de.jcup.eclipse.commons.ui.SWTFactory.*; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.PreferencePage; +import org.eclipse.jface.viewers.DoubleClickEvent; +import org.eclipse.jface.viewers.IDoubleClickListener; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ControlAdapter; +import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +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.ScrollBar; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +import de.jcup.asciidoctoreditor.ASPSupport; +import de.jcup.asciidoctoreditor.AsciiDoctorEditorActivator; +import de.jcup.asciidoctoreditor.console.AsciiDoctorConsoleUtil; +import de.jcup.asciidoctoreditor.util.AsciiDoctorEditorUtil; +import de.jcup.eclipse.commons.ui.SWTFactory; + +public class AsciiDoctorEditorEnvironmentPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { + + private Button btnCustomEnvironmentSettingsEnabled; + protected boolean customEnvironmentEntriesEnabled; + + protected TableViewer propertiesTable; + protected String[] propertyTableColumnHeaders = { "Variable", "Value", }; + protected static final String P_VARIABLE = "variable"; + protected static final String P_VALUE = "value"; + protected Button propertyAddButton; + protected Button propertyEditButton; + protected Button propertyRemoveButton; + private List definitionWorkingCopy; + private static final Object[] NO_OBJECTS = new Object[] {}; + + public AsciiDoctorEditorEnvironmentPreferencePage() { + setPreferenceStore(AsciiDoctorEditorUtil.getPreferences().getPreferenceStore()); + setDescription("Setup custom environment entries."); + setTitle("Customize"); + this.definitionWorkingCopy = new ArrayList(); + } + + @Override + protected void performDefaults() { + this.definitionWorkingCopy = new ArrayList<>(); + updateTable(); + } + + private CustomEnvironmentEntrySupport getSupport() { + return CustomEnvironmentEntrySupport.DEFAULT; + } + + @Override + public boolean performOk() { + + boolean enabledStateAsBefore = customEnvironmentEntriesEnabled == getSupport().areCustomEnvironmentEntriesEnabled(); + boolean sameContentAsBefore = checkSameDefinitionsAsBefore(); + if (!sameContentAsBefore || !enabledStateAsBefore) { + + getSupport().setCustomEnvironmentEntriesEnabled(customEnvironmentEntriesEnabled); + getSupport().setCustomEnvironmentEntries(definitionWorkingCopy); + + if (! AsciiDoctorEditorPreferences.getInstance().isUsingInstalledAsciidoctor()) { + + ASPSupport aspSupport = AsciiDoctorEditorActivator.getDefault().getAspSupport(); + if (aspSupport.stop()) { + AsciiDoctorConsoleUtil.showConsole(); + AsciiDoctorConsoleUtil.output("Stopped server because of changed preferences - will restart with new environment settings"); + + aspSupport.configurationChanged(); + } + } + } + return super.performOk(); + } + + private boolean checkSameDefinitionsAsBefore() { + /* we use a set to check for changes - list does inspect ordering too! */ + Set freshCopy = getSupport().fetchConfiguredEnvironmentEntriesData(); + HashSet copiedDefinitionsAsSet = new HashSet<>(definitionWorkingCopy); + + return copiedDefinitionsAsSet.equals(freshCopy); + } + + @Override + protected void performApply() { + super.performApply(); + } + + @Override + public void dispose() { + super.dispose(); + } + + @Override + public void init(IWorkbench workbench) { + } + + @Override + protected Control createContents(Composite parent) { + Composite mainComposite = SWTFactory.createComposite(parent, 2, 2, GridData.FILL_BOTH); + + customEnvironmentEntriesEnabled = getSupport().areCustomEnvironmentEntriesEnabled(); + definitionWorkingCopy.addAll(getSupport().fetchConfiguredEnvironmentEntriesData()); + + createCheckBox(mainComposite); + + createTable(mainComposite); + createTableButtons(mainComposite); + + return mainComposite; + } + + private void createCheckBox(Composite mainComposite) { + btnCustomEnvironmentSettingsEnabled = new Button(mainComposite, SWT.CHECK); + btnCustomEnvironmentSettingsEnabled.setText("Custom entries enabled"); + btnCustomEnvironmentSettingsEnabled.setSelection(customEnvironmentEntriesEnabled); + btnCustomEnvironmentSettingsEnabled.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + updateTable(); + customEnvironmentEntriesEnabled = btnCustomEnvironmentSettingsEnabled.getSelection(); + } + }); + GridData layoutData = new GridData(); + layoutData.verticalSpan = 2; + btnCustomEnvironmentSettingsEnabled.setLayoutData(layoutData); + + } + + /** + * Content provider for the environment table + */ + protected class TaskTagDefinitionsContentProvider implements IStructuredContentProvider { + + public Object[] getElements(Object inputElement) { + if (inputElement instanceof List) { + Object[] result = ((List) inputElement).toArray(); + return result; + } + return NO_OBJECTS; + } + + public void dispose() { + } + + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + if (newInput == null) { + return; + } + if (viewer instanceof TableViewer) { + TableViewer tableViewer = (TableViewer) viewer; + if (tableViewer.getTable().isDisposed()) { + return; + } + tableViewer.refresh(); + } + } + } + + /** + * Label provider for the table + */ + public class TaskTagDefinitionLabelProvider extends LabelProvider implements ITableLabelProvider { + public String getColumnText(Object element, int columnIndex) { + String result = null; + if (element != null) { + KeyValue var = (KeyValue) element; + switch (columnIndex) { + case 0: // variable + result = var.getKey(); + break; + case 1: // value + result = var.getValue(); + break; + } + } + return result; + } + + public Image getColumnImage(Object element, int columnIndex) { + return null; + } + } + + public void createControl(Composite parent) { + super.createControl(parent); + } + + /** + * Creates and configures the table that displayed the key/value pairs that + * comprise the environment. + * + * @param parent the composite in which the table should be created + */ + protected void createTable(Composite parent) { + Font font = parent.getFont(); + SWTFactory.createLabel(parent, "Definitions:", 2); + // Create table composite + Composite tableComposite = SWTFactory.createComposite(parent, font, 1, 1, GridData.FILL_BOTH, 0, 0); + // Create table + propertiesTable = new TableViewer(tableComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION); + Table table = propertiesTable.getTable(); + table.setLayout(new GridLayout()); + table.setLayoutData(new GridData(GridData.FILL_BOTH)); + table.setHeaderVisible(true); + table.setLinesVisible(true); + table.setFont(font); + propertiesTable.setContentProvider(new TaskTagDefinitionsContentProvider()); + propertiesTable.setLabelProvider(new TaskTagDefinitionLabelProvider()); + propertiesTable.setColumnProperties(new String[] { P_VARIABLE, P_VALUE }); + propertiesTable.setComparator(new ViewerComparator() { + public int compare(Viewer iviewer, Object e1, Object e2) { + if (e1 == null) { + return -1; + } else if (e2 == null) { + return 1; + } else { + return ((KeyValue) e1).getKey().compareToIgnoreCase(((KeyValue) e2).getKey()); + } + } + }); + propertiesTable.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + handleTableSelectionChanged(event); + } + }); + propertiesTable.addDoubleClickListener(new IDoubleClickListener() { + public void doubleClick(DoubleClickEvent event) { + if (!propertiesTable.getSelection().isEmpty()) { + handlePropertiesEditButtonSelected(); + } + } + }); + // Create columns + final TableColumn tc1 = new TableColumn(table, SWT.NONE, 0); + tc1.setText(propertyTableColumnHeaders[0]); + final TableColumn tc2 = new TableColumn(table, SWT.NONE, 1); + tc2.setText(propertyTableColumnHeaders[1]); + final Table tref = table; + final Composite comp = tableComposite; + tableComposite.addControlListener(new ControlAdapter() { + public void controlResized(ControlEvent e) { + Rectangle area = comp.getClientArea(); + Point size = tref.computeSize(SWT.DEFAULT, SWT.DEFAULT); + ScrollBar vBar = tref.getVerticalBar(); + int width = area.width - tref.computeTrim(0, 0, 0, 0).width - 2; + if (size.y > area.height + tref.getHeaderHeight()) { + Point vBarSize = vBar.getSize(); + width -= vBarSize.x; + } + Point oldSize = tref.getSize(); + if (oldSize.x > area.width) { + tc1.setWidth(width / 2 - 1); + tc2.setWidth(width - tc1.getWidth()); + tref.setSize(area.width, area.height); + } else { + tref.setSize(area.width, area.height); + tc1.setWidth(width / 2 - 1); + tc2.setWidth(width - tc1.getWidth()); + } + } + }); + propertiesTable.setInput(definitionWorkingCopy); + propertiesTable.refresh(); + } + + /** + * Responds to a selection changed event in the table + * + * @param event the selection change event + */ + protected void handleTableSelectionChanged(SelectionChangedEvent event) { + int size = ((IStructuredSelection) event.getSelection()).size(); + propertyEditButton.setEnabled(size == 1); + propertyRemoveButton.setEnabled(size > 0); + } + + /** + * Creates the add/edit/remove buttons for the table + * + * @param parent the composite in which the buttons should be created + */ + protected void createTableButtons(Composite parent) { + // Create button composite + Composite buttonComposite = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END, 0, 0); + + // Create buttons + propertyAddButton = createPushButton(buttonComposite, "N&ew...", null); + propertyAddButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + handlePropertiesAddButtonSelected(); + } + }); + propertyEditButton = createPushButton(buttonComposite, "E&dit...", null); + propertyEditButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + handlePropertiesEditButtonSelected(); + } + }); + propertyEditButton.setEnabled(false); + propertyRemoveButton = createPushButton(buttonComposite, "Rem&ove", null); + propertyRemoveButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + handlePropertiesRemoveButtonSelected(); + } + }); + propertyRemoveButton.setEnabled(false); + } + + /** + * Adds a new variable to the table. + */ + protected void handlePropertiesAddButtonSelected() { + KeyValueEntryDialog dialog = new KeyValueEntryDialog(getShell(), null, "Add new environment variable", null); + if (dialog.open() != Window.OK) { + return; + } + KeyValue keyValue = dialog.getKeyValue(); + String name = keyValue.getKey(); + + if (name != null && name.length() > 0) { + addVariable(keyValue); + } + } + + /** + * Attempts to add the given variable. Returns whether the variable was added or + * not (as when the user answers not to overwrite an existing variable). + * + * @param definition the variable to add + * @return true when variable was added + */ + protected boolean addVariable(KeyValue definition) { + String key = definition.getKey(); + TableItem[] items = propertiesTable.getTable().getItems(); + for (int i = 0; i < items.length; i++) { + KeyValue existingVariable = (KeyValue) items[i].getData(); + if (existingVariable.getKey().equals(key)) { + boolean overWrite = MessageDialog.openQuestion(getShell(), "Overwrite environment definition?", + MessageFormat.format("An environment definition named {0} already exists. Overwrite?", new Object[] { key })); // + if (!overWrite) { + return false; + } + definitionWorkingCopy.remove(existingVariable); + break; + } + } + definitionWorkingCopy.add(definition); + updateTable(); + return true; + } + + private void updateTable() { + this.propertiesTable.getContentProvider().inputChanged(propertiesTable, null, definitionWorkingCopy); + + boolean todosEnabled = btnCustomEnvironmentSettingsEnabled.getSelection(); + + propertiesTable.getControl().setEnabled(todosEnabled); + propertyAddButton.setEnabled(todosEnabled); + if (!todosEnabled) { + propertiesTable.setSelection(null); + } + + } + + private void handlePropertiesEditButtonSelected() { + IStructuredSelection sel = (IStructuredSelection) propertiesTable.getSelection(); + KeyValue var = (KeyValue) sel.getFirstElement(); + if (var == null) { + return; + } + + KeyValueEntryDialog dialog = new KeyValueEntryDialog(getShell(), var, "Change environment entry", null); + if (dialog.open() != Window.OK) { + return; + } + updateTable(); + } + + private void handlePropertiesRemoveButtonSelected() { + IStructuredSelection sel = (IStructuredSelection) propertiesTable.getSelection(); + for (@SuppressWarnings("unchecked") + Iterator i = sel.iterator(); i.hasNext();) { + KeyValue var = i.next(); + definitionWorkingCopy.remove(var); + } + updateTable(); + } + +} \ No newline at end of file diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/CustomEnvironmentEntrySupport.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/CustomEnvironmentEntrySupport.java new file mode 100644 index 0000000..c6e6ce4 --- /dev/null +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/CustomEnvironmentEntrySupport.java @@ -0,0 +1,56 @@ +package de.jcup.asciidoctoreditor.preferences; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.eclipse.jface.preference.IPreferenceStore; + +import de.jcup.asciidoctoreditor.util.AsciiDoctorEditorUtil; + +public class CustomEnvironmentEntrySupport { + + public static CustomEnvironmentEntrySupport DEFAULT = new CustomEnvironmentEntrySupport(); + + public final static String PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_DATA = "custom.env.entries.data";; + public final static String PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_ENABLED = "custom.env.entries.enabled"; + + private IPreferenceStore getPreferenceStore() { + return AsciiDoctorEditorUtil.getPreferences().getPreferenceStore(); + } + + public Set fetchConfiguredEnvironmentEntriesData() { + String data = getPreferenceStore().getString(PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_DATA); + HashSet freshCopy = new HashSet<>(KeyValueConverter.DEFAULT.convertStringToList(data)); + return freshCopy; + } + + public boolean areCustomEnvironmentEntriesEnabled() { + return getPreferenceStore().getBoolean(PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_ENABLED); + } + + public void setCustomEnvironmentEntriesEnabled(boolean enabled) { + getPreferenceStore().setValue(PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_ENABLED, enabled); + } + + public void setCustomEnvironmentEntries(List definitionWorkingCopy) { + String convertListTostring = KeyValueConverter.DEFAULT.convertListTostring(definitionWorkingCopy); + getPreferenceStore().setValue(PREFERENCE_KEY_CUSTOM_ENV_ENTRIES_DATA, convertListTostring); + } + + /** + * Fetches custom environment entries + * @return + */ + public Map fetchConfiguredEnvironmentEntriesAsMap(){ + HashMap map = new HashMap<>(); + for (KeyValue keyValue: fetchConfiguredEnvironmentEntriesData()) { + map.put(keyValue.getKey(),keyValue.getValue()); + } + return map; + } + + +} diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValue.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValue.java new file mode 100644 index 0000000..f9331c1 --- /dev/null +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValue.java @@ -0,0 +1,56 @@ +package de.jcup.asciidoctoreditor.preferences; + +import java.util.Objects; + +public class KeyValue { + + private String key; + private String value; + + public KeyValue(String key, String value) { + setKey(key); + setValue(value); + } + + public void setKey(String key) { + if (key == null) { + key = ""; + } + this.key = key; + } + + public void setValue(String value) { + if (value == null) { + value = ""; + } + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof KeyValue)) { + return false; + } + KeyValue other = (KeyValue) obj; + return Objects.equals(key, other.key) && Objects.equals(value, other.value); + } +} diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueConverter.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueConverter.java new file mode 100644 index 0000000..ae86172 --- /dev/null +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueConverter.java @@ -0,0 +1,24 @@ +package de.jcup.asciidoctoreditor.preferences; + +import de.jcup.eclipse.commons.preferences.AbstractPreferenceValueConverter; + +public class KeyValueConverter extends AbstractPreferenceValueConverter{ + + public static final KeyValueConverter DEFAULT = new KeyValueConverter(); + + @Override + protected void write(KeyValue oneEntry, + de.jcup.eclipse.commons.preferences.PreferenceDataWriter writer) { + writer.writeString(oneEntry.getKey()); + writer.writeString(oneEntry.getValue()); + } + + @Override + protected KeyValue read( + de.jcup.eclipse.commons.preferences.PreferenceDataReader reader) { + KeyValue definition = new KeyValue(reader.readString(),reader.readString()); + return definition; + } + + +} \ No newline at end of file diff --git a/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueEntryDialog.java b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueEntryDialog.java new file mode 100644 index 0000000..1ed4276 --- /dev/null +++ b/asciidoctor-editor-plugin/src/main/java-eclipse/de/jcup/asciidoctoreditor/preferences/KeyValueEntryDialog.java @@ -0,0 +1,100 @@ +package de.jcup.asciidoctoreditor.preferences; + +import org.eclipse.jface.dialogs.IMessageProvider; +import org.eclipse.jface.dialogs.TitleAreaDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +class KeyValueEntryDialog extends TitleAreaDialog { + + private Text txtKey; + private Text txtValue; + private String title; + private String message; + private KeyValue keyValue; + + public KeyValueEntryDialog(Shell parentShell, KeyValue keyValue, String title, String message) { + super(parentShell); + this.title = title; + this.message = message; + if (keyValue == null) { + keyValue = new KeyValue("",""); + } + this.keyValue = keyValue; + } + + @Override + public void create() { + super.create(); + setTitle(title); + if (message != null) { + setMessage(message, IMessageProvider.INFORMATION); + } + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite area = (Composite) super.createDialogArea(parent); + Composite container = new Composite(area, SWT.NONE); + container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + GridLayout layout = new GridLayout(2, false); + container.setLayout(layout); + + createKeyTextfields(container); + createValueTextfields(container); + + return area; + } + + private void createKeyTextfields(Composite container) { + Label lblIdentifier = new Label(container, SWT.NONE); + lblIdentifier.setText("Key"); + + GridData data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.horizontalAlignment = GridData.FILL; + + txtKey = new Text(container, SWT.BORDER); + txtKey.setLayoutData(data); + txtKey.setText(keyValue.getKey()); + } + + private void createValueTextfields(Composite container) { + Label lblIdentifier = new Label(container, SWT.NONE); + lblIdentifier.setText("Identifier"); + + GridData data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.horizontalAlignment = GridData.FILL; + + txtValue = new Text(container, SWT.BORDER); + txtValue.setLayoutData(data); + txtValue.setText(keyValue.getValue()); + } + + @Override + protected boolean isResizable() { + return true; + } + + private void saveInput() { + keyValue.setKey(txtKey.getText()); + keyValue.setValue(txtValue.getText()); + } + + @Override + protected void okPressed() { + saveInput(); + super.okPressed(); + } + + public KeyValue getKeyValue() { + return keyValue; + } +} diff --git a/asciidoctor-editor-plugin/src/main/java/de/jcup/asciidoctoreditor/asciidoc/ASPServerAdapter.java b/asciidoctor-editor-plugin/src/main/java/de/jcup/asciidoctoreditor/asciidoc/ASPServerAdapter.java index a978392..08b8222 100644 --- a/asciidoctor-editor-plugin/src/main/java/de/jcup/asciidoctoreditor/asciidoc/ASPServerAdapter.java +++ b/asciidoctor-editor-plugin/src/main/java/de/jcup/asciidoctoreditor/asciidoc/ASPServerAdapter.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.net.ServerSocket; +import java.util.Map; import java.util.Objects; import de.jcup.asciidoctoreditor.ConsoleAdapter; @@ -44,6 +45,7 @@ public class ASPServerAdapter { private ExternalProcessAsciidoctorJServerLauncher launcher; private boolean showServerOutput; private boolean showCommunication; + private Map customEnvironmentEntries; public ASPServerAdapter() { } @@ -86,9 +88,13 @@ public void setShowServerOutput(boolean showServerOutput) { } } + public void setCustomEnvironmentEntries(Map map) { + this.customEnvironmentEntries = map; + } + public void setShowCommunication(boolean showCommunication) { this.showCommunication = showCommunication; - if (client!=null) { + if (client != null) { client.setShowCommunication(showCommunication); } } @@ -108,12 +114,7 @@ public void startServer() { if (launcher != null) { launcher.stopServer(); } - this.port = getFreePortToUse(minPort, maxPort); - - launcher = new ExternalProcessAsciidoctorJServerLauncher(pathToServerJar, port); - launcher.setPathToJavaBinary(pathToJavaBinary); - launcher.setShowServerOutput(showServerOutput); - launcher.setLogHandler(new LogHandler() { + LogHandler logHandler = new LogHandler() { @Override public void error(String message, Throwable t) { @@ -122,7 +123,7 @@ public void error(String message, Throwable t) { } } - }); + }; OutputHandler outputHandler = new OutputHandler() { @Override @@ -133,12 +134,28 @@ public void output(String message) { }; }; + this.port = getFreePortToUse(minPort, maxPort); + + launcher = new ExternalProcessAsciidoctorJServerLauncher(pathToServerJar, port); + launcher.setPathToJavaBinary(pathToJavaBinary); + launcher.setShowServerOutput(showServerOutput); + launcher.setLogHandler(logHandler); launcher.setOutputHandler(outputHandler); + + if (customEnvironmentEntries != null) { + for (String key: customEnvironmentEntries.keySet()) { + launcher.setEnvironment(key, customEnvironmentEntries.get(key)); + } + } + try { String key = launcher.launch(30); - /* Next line is a temporary workaround until fixed in ASP itself - on windows we got \r\n so having \r inside key... we must trim to avoid the problem */ + /* + * Next line is a temporary workaround until fixed in ASP itself - on windows we + * got \r\n so having \r inside key... we must trim to avoid the problem + */ key = key.trim(); - + outputHandler.output(">> ASP Server has been started successfully"); this.client = new AspClient(key); this.client.setPortNumber(port);