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

Commit

Permalink
Added StatusMessageDialog(2)
Browse files Browse the repository at this point in the history
with support for additional help message with links.
  • Loading branch information
bruno-medeiros committed Jul 6, 2016
1 parent 1965575 commit 3292780
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

import melnorme.lang.ide.core.EclipseCore;
import melnorme.lang.ide.core.LangCore;
import melnorme.lang.tooling.common.ops.Operation;
import melnorme.lang.tooling.common.ops.IOperationMonitor;
import melnorme.lang.tooling.common.ops.IOperationMonitor.BasicOperationMonitor;
import melnorme.lang.tooling.common.ops.Operation;
import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
Expand Down Expand Up @@ -112,7 +112,7 @@ public static void removeNature(IProject project, String natureID) throws CoreEx
}

public static <T> T getAdapter(Object adaptable, Class<T> adapterType) {
return (T) Platform.getAdapterManager().getAdapter(adaptable, adapterType);
return Platform.getAdapterManager().getAdapter(adaptable, adapterType);
}

/* ----------------- status ----------------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class BooleanPreference extends PreferenceHelper<Boolean> {
public BooleanPreference(String key, boolean defaultValue) {
super(key, defaultValue);
}
public BooleanPreference(String key, boolean defaultValue, IProjectPreference<Boolean> useProjectSettings) {
super(key, defaultValue, useProjectSettings);
}

public BooleanPreference(String pluginId, String key, boolean defaultValue) {
super(pluginId, key, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class DerivedValuePreference<VALUE> {
protected final StringPreference preference;
protected final Validator<String, VALUE> validator;

public DerivedValuePreference(String pluginId, String key, String defaultValue,
public DerivedValuePreference(String key, String defaultValue,
IProjectPreference<Boolean> useProjectPref /* can be null*/,
Validator<String, VALUE> validator) {
this(new StringPreference(pluginId, key, defaultValue, useProjectPref), validator);
this(new StringPreference(key, defaultValue, useProjectPref), validator);
}

public DerivedValuePreference(StringPreference preference, Validator<String, VALUE> validator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/
public class OptionalStringPreference extends StringPreference {

public OptionalStringPreference(String pluginId, String key,
IProjectPreference<Boolean> useProjectSettings) {
super(pluginId, key, null, useProjectSettings);
public OptionalStringPreference(String key, IProjectPreference<Boolean> useProjectSettings) {
super(key, null, useProjectSettings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ public PreferenceHelper(String pluginId, String key, T defaultValue) {
this(pluginId, key, defaultValue, true, null);
}

public PreferenceHelper(String pluginId, String key, T defaultValue,
IProjectPreference<Boolean> useProjectPref) {
public PreferenceHelper(String pluginId, String key, T defaultValue, IProjectPreference<Boolean> useProjectPref) {
this(pluginId, key, defaultValue, true, useProjectPref);
}

public PreferenceHelper(String key, T defaultValue, IProjectPreference<Boolean> useProjectPref) {
this(LangCore.PLUGIN_ID, key, defaultValue, true, useProjectPref);
}

public PreferenceHelper(String pluginId, String key, T defaultValue, boolean ensureUniqueKey,
IProjectPreference<Boolean> useProjectPreference) {
this.key = assertNotNull(PreferencesOverride.getKeyIdentifer(key, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class StringPreference extends PreferenceHelper<String> {
public StringPreference(String key, String defaultValue) {
super(key, defaultValue);
}
public StringPreference(String key, String defaultValue, IProjectPreference<Boolean> useProjectPref) {
super(key, defaultValue, useProjectPref);
}
public StringPreference(String pluginId, String key, String defaultValue) {
super(pluginId, key, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
Expand All @@ -29,14 +28,14 @@
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.PreferencesUtil;

import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.LangCore_Actual;
import melnorme.lang.ide.core.utils.EclipseUtils;
import melnorme.lang.ide.core.utils.ProjectValidator;
import melnorme.lang.ide.core.utils.ResourceUtils;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
import melnorme.lang.ide.ui.utils.WorkbenchUtils;
import melnorme.util.swt.SWTFactory;
import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.SWTUtil;
Expand Down Expand Up @@ -310,13 +309,8 @@ public void widgetSelected(SelectionEvent e) {
}

protected void openPreferencePage() {
PreferenceDialog pref = PreferencesUtil.createPreferenceDialogOn(getShell(),
LangUIPlugin_Actual.ROOT_PREF_PAGE_ID, null, null);

if (pref != null) {
pref.open();
updateWidgetFromInput();
}
WorkbenchUtils.openPreferencePage(getShell(), LangUIPlugin_Actual.ROOT_PREF_PAGE_ID);
updateWidgetFromInput();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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.Shell;

import melnorme.util.swt.SWTFactory;
Expand Down Expand Up @@ -56,7 +55,7 @@ protected void configureShell(Shell newShell) {

@Override
protected Image getImage() {
return Display.getCurrent().getSystemImage(SWTUtil.getSystemImageCode(statusLevel));
return SWTUtil.getSWTImageForStatusLevel(statusLevel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*******************************************************************************
* Copyright (c) 2016 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.utils;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.SWTUtil;
import melnorme.util.swt.components.AbstractWidget;
import melnorme.utilbox.status.StatusException;

public class StatusMessageDialog2 extends Dialog {

protected final String title;

protected final IconAndMessageWidget iconAndMessageWidget;

public StatusMessageDialog2(Shell shell, String title, StatusException statusMessage) {
super(shell);
assertNotNull(statusMessage);
this.title = title;
this.iconAndMessageWidget = createIconAndMessageWidget();
this.iconAndMessageWidget.setStatusMessage(statusMessage);
}

@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText(title);
}

@Override
protected Control createContents(Composite parent) {
return super.createContents(parent);
}

@Override
protected Control createDialogArea(Composite parent) {
Composite topControl = (Composite) super.createDialogArea(parent);
GridLayout gridLayout = (GridLayout) topControl.getLayout();
gridLayout.numColumns = 2;
iconAndMessageWidget.createComponentInlined(topControl);
iconAndMessageWidget.messageControlLayoutData().widthHint =
convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);

return topControl;
}

protected IconAndMessageWidget createIconAndMessageWidget() {
return new IconAndMessageWidget();
}

public static class IconAndMessageWidget extends AbstractWidget {

protected StatusException statusMessage;

protected Label imageControl;
protected Label messageControl;

public IconAndMessageWidget() {
}

@Override
public int getPreferredLayoutColumns() {
return 2;
}

public void setStatusMessage(StatusException statusMessage) {
this.statusMessage = assertNotNull(statusMessage);
}

@Override
protected void createContents(Composite topControl) {
imageControl = createImageControl(topControl);
createMessageControl(topControl);
}

protected Label createImageControl(Composite topControl) {
return SWTFactoryUtil.createIconLabel(topControl, getImage(),
GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING).create()
);
}

protected Image getImage() {
return SWTUtil.getImageForSeverity(statusMessage.getSeverity());
}

protected void createMessageControl(Composite topControl) {
String message = statusMessage.getMultiLineRender();
messageControl = SWTFactoryUtil.createLabel(topControl, SWT.WRAP, message, gdGrabAll());
}

public GridData messageControlLayoutData() {
assertNotNull(messageControl);
return (GridData) messageControl.getLayoutData();
}

@Override
protected void updateWidgetFromInput() {
// immutable widget
}
}

@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2016 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.utils;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

import melnorme.util.swt.SWTFactoryUtil;
import melnorme.utilbox.fields.validation.IDetailsMessage;
import melnorme.utilbox.misc.StringUtil;
import melnorme.utilbox.status.StatusException;

public class StatusMessageDialogExt extends StatusMessageDialog2 {

public StatusMessageDialogExt(Shell shell, String title, StatusException statusMessage) {
super(shell, title, statusMessage);
}

@Override
protected IconAndMessageWidget createIconAndMessageWidget() {
return new IconAndMessageWidgetExt();
}

/**
* Extension to {@link IconAndMessageWidget}, allows displaying additional info from {@link IDetailsMessage}
* in a control with links in texts.
*/
public static class IconAndMessageWidgetExt extends IconAndMessageWidget {

protected Link helpControl;

@Override
protected void createMessageControl(Composite topControl) {
super.createMessageControl(topControl);

if(statusMessage instanceof IDetailsMessage) {
IDetailsMessage detailsMessage = (IDetailsMessage) statusMessage;
createDetailsMessage(topControl, detailsMessage);
}
}

protected void createDetailsMessage(Composite topControl, IDetailsMessage detailsMessage) {
String additionalMessage = "\n" + detailsMessage.getDetailsMessage();

helpControl = SWTFactoryUtil.createLink(topControl, SWT.WRAP, additionalMessage,
gdfFillDefaults().grab(true, true).span(2, 1).create()
);

helpControl.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String uri = e.text;
if(uri.startsWith("http")) {
Program.launch(uri);
} else if(uri.startsWith("pref:")) {
String prefId = StringUtil.removeStart("pref:", uri);
WorkbenchUtils.openPreferencePage(helpControl.getShell(), prefId);
} else {
UIOperationsStatusHandler.handleInternalError("Unknown link URI:\n" + uri, null);
}
}
});
}

public Link getHelpControl() {
return helpControl;
}
}

}
Loading

0 comments on commit 3292780

Please sign in to comment.