001: /*
002: * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. The name of the author may not be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package jfb.tools.activitymgr.ui.dialogs;
029:
030: import org.apache.log4j.Logger;
031: import org.eclipse.jface.dialogs.Dialog;
032: import org.eclipse.jface.dialogs.MessageDialog;
033: import org.eclipse.swt.graphics.Image;
034: import org.eclipse.swt.layout.GridLayout;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Control;
037: import org.eclipse.swt.widgets.Shell;
038: import org.eclipse.swt.widgets.Text;
039:
040: /**
041: * Dialogue père des dialogues asociés à la page de
042: * configuration.
043: */
044: public abstract class AbstractDialog extends Dialog {
045:
046: /** Logger */
047: private static Logger log = Logger.getLogger(AbstractDialog.class);
048:
049: /** Titre du dialogue */
050: private String title;
051:
052: /** Icone du dialogue */
053: private Image icon;
054:
055: /** Valeur initiale */
056: private Object initialValue;
057:
058: /** Valeur validée */
059: private Object value;
060:
061: /**
062: * Constructeur par défaut.
063: * @param parentShell le shell parent.
064: * @param title titre du dialogue.
065: * @param icon icone du dialogue.
066: * @param initialValue valeur initiale du dialogue.
067: */
068: protected AbstractDialog(Shell parentShell, String title,
069: Image icon, Object initialValue) {
070: super (parentShell);
071: this .title = title;
072: this .initialValue = initialValue;
073: this .icon = icon;
074: }
075:
076: /**
077: * Retourne la valeur saisie au travers du dialogue.
078: * @return la valeur saisie au travers du dialogue.
079: */
080: public Object getValue() {
081: return value;
082: }
083:
084: /**
085: * Définit la valeur sélectionnée par le dialogue.
086: * @param value la nouvelle valeur.
087: */
088: public void setValue(Object value) {
089: this .value = value;
090: }
091:
092: /**
093: * Retourne la valeur initiale du dialogue.
094: * @return la valeur initiale du dialogue.
095: */
096: public Object getInitialValue() {
097: return initialValue;
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
102: */
103: protected void okPressed() {
104: try {
105: log.debug("Ok pressed");
106: value = validateUserEntry();
107: super .okPressed();
108: } catch (DialogException e) {
109: Control control = e.getControl();
110: if (control instanceof Text) {
111: Text text = (Text) control;
112: text.setFocus();
113: text.selectAll();
114: }
115: // Affichage du message d'erreur
116: MessageDialog.openWarning(getShell(), "Error", e
117: .getMessage());
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
123: */
124: protected void cancelPressed() {
125: log.debug("Cancel pressed");
126: super .cancelPressed();
127: }
128:
129: /**
130: * Valide la saisie de l'utilisateur.
131: * @return la nouvelle valeur du dialogue.
132: * @throws DialogException levé en cas de détection d'anomalie
133: * dans la saisie de l'utilisateur.
134: */
135: protected abstract Object validateUserEntry()
136: throws DialogException;
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
140: */
141: protected Control createDialogArea(Composite parent) {
142: Composite c = (Composite) super .createDialogArea(parent);
143: Shell shell = c.getShell();
144: if (title != null)
145: shell.setText(title);
146: if (icon != null)
147: shell.setImage(icon);
148:
149: // Mise à jour du Layout
150: GridLayout defaultLayout = (GridLayout) c.getLayout();
151: GridLayout layout = new GridLayout(2, false);
152: layout.marginHeight = defaultLayout.marginHeight;
153: layout.marginWidth = defaultLayout.marginWidth;
154: layout.verticalSpacing = defaultLayout.verticalSpacing;
155: layout.horizontalSpacing = defaultLayout.horizontalSpacing;
156: c.setLayout(layout);
157:
158: // Retour du résultat
159: return c;
160: }
161:
162: }
|