001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.ui;
016:
017: import org.eclipse.jface.viewers.CellEditor;
018: import org.eclipse.jface.viewers.ICellEditorListener;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.graphics.Color;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.ui.IActionBars;
024: import org.eclipse.ui.IEditorPart;
025: import org.eclipse.ui.IViewPart;
026: import org.eclipse.ui.IWorkbenchPage;
027: import org.eclipse.ui.IWorkbenchPart;
028: import org.eclipse.ui.IWorkbenchWindow;
029: import org.eclipse.ui.PlatformUI;
030:
031: /**
032: * Writes a message to the Status Line if available and changes the BackgroundColor of the editor if possible.
033: *
034: * @author Jesse
035: * @since 1.1.0
036: */
037: public class DisplayErrorCellListener implements ICellEditorListener {
038:
039: private CellEditor editor;
040: private Color validBackgroundColor;
041: private Color validForegroundColor;
042:
043: public DisplayErrorCellListener(CellEditor editor) {
044: this .editor = editor;
045: }
046:
047: public void applyEditorValue() {
048: setStatusLineMessage(false, true);
049: setEditorBackground(false, true);
050: }
051:
052: public void cancelEditor() {
053: setStatusLineMessage(false, true);
054: setEditorBackground(false, true);
055: }
056:
057: public void editorValueChanged(boolean oldValidState,
058: boolean newValidState) {
059: setStatusLineMessage(oldValidState, newValidState);
060: setEditorBackground(oldValidState, newValidState);
061: }
062:
063: private void setEditorBackground(boolean oldValidState,
064: boolean newValidState) {
065: if (oldValidState == newValidState)
066: return;
067: Control control = editor.getControl();
068:
069: if (control == null)
070: return;
071:
072: if (oldValidState) {
073:
074: if (validBackgroundColor == null) {
075: this .validBackgroundColor = control.getBackground();
076: }
077: if (validBackgroundColor == null) {
078: this .validForegroundColor = control.getForeground();
079: }
080: }
081:
082: if (newValidState) {
083: if (validBackgroundColor == null) {
084: control.setBackground(Display.getCurrent()
085: .getSystemColor(SWT.COLOR_LIST_BACKGROUND));
086: } else {
087: control.setBackground(validBackgroundColor);
088: }
089: if (validForegroundColor == null) {
090: control.setForeground(Display.getCurrent()
091: .getSystemColor(SWT.COLOR_BLACK));
092: } else {
093: control.setForeground(validBackgroundColor);
094: }
095: } else {
096: control.setBackground(Display.getCurrent().getSystemColor(
097: SWT.COLOR_RED));
098: control.setForeground(Display.getCurrent().getSystemColor(
099: SWT.COLOR_WHITE));
100: }
101: }
102:
103: private void setStatusLineMessage(boolean oldValidState,
104: boolean newValidState) {
105:
106: IWorkbenchWindow activeWorkbenchWindow = PlatformUI
107: .getWorkbench().getActiveWorkbenchWindow();
108: if (activeWorkbenchWindow == null)
109: return;
110:
111: IWorkbenchPage activePage = activeWorkbenchWindow
112: .getActivePage();
113: if (activePage == null)
114: return;
115:
116: IWorkbenchPart activePart = activePage.getActivePart();
117: if (activePart == null)
118: return;
119:
120: IActionBars actionBars;
121: if (activePart instanceof IViewPart) {
122: actionBars = ((IViewPart) activePart).getViewSite()
123: .getActionBars();
124: } else if (activePart instanceof IEditorPart) {
125: actionBars = ((IEditorPart) activePart).getEditorSite()
126: .getActionBars();
127: } else {
128: return;
129: }
130:
131: if (newValidState) {
132: actionBars.getStatusLineManager().setErrorMessage(null);
133: } else {
134: actionBars.getStatusLineManager().setErrorMessage(
135: editor.getErrorMessage());
136: }
137: }
138:
139: }
|