Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
LANG: fully reviewed getNewValueFromButtonSelection2
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Aug 12, 2015
1 parent 216add1 commit a0a19de
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.dialogs.PreferencesUtil;
Expand All @@ -49,8 +48,10 @@
import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.SWTUtil;
import melnorme.util.swt.components.AbstractComponent;
import melnorme.util.swt.components.fields.DirectoryTextField;
import melnorme.util.swt.components.fields.EnablementButtonTextField;
import melnorme.util.swt.components.fields.TextFieldComponent;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.fields.IFieldValueListener;
import melnorme.utilbox.misc.StringUtil;
Expand Down Expand Up @@ -277,12 +278,8 @@ protected void createContents_Label(Composite parent) {
}

@Override
protected String getNewValueFromButtonSelection() {
DirectoryDialog dialog = new DirectoryDialog(button.getShell());
if(!getFieldValue().isEmpty()) {
dialog.setFilterPath(getFieldValue());
}
return dialog.open();
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return DirectoryTextField.openDirectoryDialog(getFieldValue(), button.getShell());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void createContents_layout() {
/* ----------------- ----------------- */

@Override
protected String getNewValueFromButtonSelection() throws OperationCancellation {
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return getFieldValue() + ControlUtils.openStringVariableSelectionDialog(text.getShell());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.components.fields.ButtonTextField;
import melnorme.utilbox.concurrency.OperationCancellation;

/**
* A field whose main value is a project name from the Eclipse workspace.
Expand Down Expand Up @@ -69,12 +70,12 @@ protected void createContents_Label(Composite parent) {
/* ----------------- ----------------- */

@Override
protected String getNewValueFromButtonSelection() {
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
IProject project = chooseProject();
return project == null ? null : project.getName();
}

protected IProject chooseProject() {
protected IProject chooseProject() throws OperationCancellation {
Shell shell = button.getShell();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new WorkbenchLabelProvider());
dialog.setTitle(LangUIMessages.projectField_chooseProject_title);
Expand All @@ -95,8 +96,7 @@ protected IProject chooseProject() {
if (dialog.open() == Window.OK) {
return (IProject) dialog.getFirstResult();
}

return null;
throw new OperationCancellation();
}

protected IProject[] getDialogChooseElements() throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.ide.ui.utils.ControlUtils;
import melnorme.util.swt.components.fields.EnablementButtonTextField;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.core.fntypes.CommonGetter;

Expand All @@ -38,7 +39,7 @@ public ProjectRelativePathField(String labelText, String useDefaultField_Label,
}

@Override
protected String getNewValueFromButtonSelection() throws CommonException {
protected String getNewValueFromButtonSelection2() throws CommonException, OperationCancellation {
return ControlUtils.openProgramPathDialog(projectGetter.get(), button);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected String getDefaultFieldValue() throws CommonException {
}

@Override
protected String getNewValueFromButtonSelection() throws OperationCancellation {
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return getFieldValue() + ControlUtils.openStringVariableSelectionDialog(text.getShell());
}

Expand All @@ -149,7 +149,7 @@ protected String getDefaultFieldValue() throws CommonException {
}

@Override
protected String getNewValueFromButtonSelection() throws CommonException, OperationCancellation {
protected String getNewValueFromButtonSelection2() throws CommonException, OperationCancellation {
return getFieldValue() + ControlUtils.openStringVariableSelectionDialog(text.getShell());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
Expand All @@ -44,6 +45,19 @@ public static void createHorizontalSpacer(Composite topControl, int charHeight,
GridDataFactory.fillDefaults().hint(pc.convertHeightInCharsToPixels(charHeight), SWT.DEFAULT).create());
}


public static String openFileDialog(String initialValue, Shell shell) throws OperationCancellation {
FileDialog dialog = new FileDialog(shell);
if(!initialValue.isEmpty()) {
dialog.setFilterPath(initialValue);
}
String result = dialog.open();
if(result == null) {
throw new OperationCancellation();
}
return result;
}

public static Link createOpenPreferencesDialogLink(final Composite topControl, String linkText) {
Link link = new Link(topControl, SWT.NONE);
link.setText(linkText);
Expand Down Expand Up @@ -76,19 +90,19 @@ public static String openStringVariableSelectionDialog(final Shell shell) throws
return result;
}

public static String openProgramPathDialog(IProject project, Button button) {
public static String openProgramPathDialog(IProject project, Button button) throws OperationCancellation {
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
button.getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
dialog.setTitle(LangUIMessages.ProgramPathDialog_title);
dialog.setMessage(LangUIMessages.ProgramPathDialog_message);

dialog.setInput(project);
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
if (dialog.open() == IDialogConstants.OK_ID) {
if(dialog.open() == IDialogConstants.OK_ID) {
IResource resource = (IResource) dialog.getFirstResult();
return resource.getProjectRelativePath().toPortableString();
}
return null;
throw new OperationCancellation();
}

public static <T> T setElementsAndOpenDialog(ElementListSelectionDialog elementListDialog, Indexable<T> configs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void widgetSelected(SelectionEvent e) {

protected void handleButtonSelected() {
try {
String result = getNewValueFromButtonSelection();
String result = getNewValueFromButtonSelection2();
if(result != null) {
setFieldValue(result);
}
Expand All @@ -93,7 +93,7 @@ protected String getButtonOperationErrorMessage() {
return "Error:";
}

protected abstract String getNewValueFromButtonSelection()
protected abstract String getNewValueFromButtonSelection2()
throws CoreException, CommonException, OperationCancellation;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;

import melnorme.utilbox.concurrency.OperationCancellation;

public class DirectoryTextField extends ButtonTextField {

public DirectoryTextField(String label) {
Expand All @@ -24,16 +26,20 @@ public DirectoryTextField(String label, String buttonlabel) {
}

@Override
protected String getNewValueFromButtonSelection() {
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return openDirectoryDialog(getFieldValue(), button.getShell());
}

public static String openDirectoryDialog(String initialValue, Shell shell) {
public static String openDirectoryDialog(String initialValue, Shell shell) throws OperationCancellation {
DirectoryDialog dialog = new DirectoryDialog(shell);
if(!initialValue.isEmpty()) {
dialog.setFilterPath(initialValue);
}
return dialog.open();
String result = dialog.open();
if(result == null) {
throw new OperationCancellation();
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* Copyright (c) 2014 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -10,8 +10,8 @@
*******************************************************************************/
package melnorme.util.swt.components.fields;

import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import melnorme.lang.ide.ui.utils.ControlUtils;
import melnorme.utilbox.concurrency.OperationCancellation;

public class FileTextField extends ButtonTextField {

Expand All @@ -26,16 +26,8 @@ public FileTextField(String label, String buttonlabel) {
}

@Override
protected String getNewValueFromButtonSelection() {
return openFileDialog(getFieldValue(), button.getShell());
}

public String openFileDialog(String initialValue, Shell shell) {
FileDialog dialog = new FileDialog(shell);
if(!initialValue.isEmpty()) {
dialog.setFilterPath(initialValue);
}
return dialog.open();
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return ControlUtils.openFileDialog(getFieldValue(), button.getShell());
}

}

0 comments on commit a0a19de

Please sign in to comment.