ProcedureMapDetailDialog.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.traceviewer.ui;
00002 
00003 import org.eclipse.jface.dialogs.Dialog;
00004 import org.eclipse.jface.dialogs.IDialogConstants;
00005 import org.eclipse.jface.dialogs.MessageDialog;
00006 import org.eclipse.jface.layout.GridDataFactory;
00007 import org.eclipse.jface.layout.GridLayoutFactory;
00008 import org.eclipse.swt.SWT;
00009 import org.eclipse.swt.events.SelectionAdapter;
00010 import org.eclipse.swt.events.SelectionEvent;
00011 import org.eclipse.swt.graphics.Image;
00012 import org.eclipse.swt.graphics.RGB;
00013 import org.eclipse.swt.layout.FillLayout;
00014 import org.eclipse.swt.widgets.Button;
00015 import org.eclipse.swt.widgets.ColorDialog;
00016 import org.eclipse.swt.widgets.Composite;
00017 import org.eclipse.swt.widgets.Control;
00018 import org.eclipse.swt.widgets.Display;
00019 import org.eclipse.swt.widgets.Label;
00020 import org.eclipse.swt.widgets.Shell;
00021 import org.eclipse.swt.widgets.Text;
00022 
00023 import edu.rice.cs.hpc.traceviewer.data.graph.ColorTable;
00024 
00025 /****
00026  * 
00027  * display procedure and its class
00028  * can be used for either adding or editing the map
00029  *
00030  */
00031 public class ProcedureMapDetailDialog extends Dialog {
00032 
00033     final private String title;
00034     private String proc;
00035     private String description;
00036     private RGB rgb;
00037     
00038     private Text txtProc;
00039     private Text txtClass;
00040 
00041     /***
00042      * retrieve the new procedure name
00043      * @return
00044      */
00045     public String getProcedure() {
00046         return proc;
00047     }
00048     
00053     public String getDescription() {
00054         return description;
00055     }
00056     
00057     public RGB getRGB() {
00058         return rgb;
00059     }
00060     
00061     /***
00062      * constructor
00063      * 
00064      * @param parentShell : parent shell
00065      * @param title : title of the dialog
00066      * @param proc : default name of the procedure
00067      * @param procClass : default name of the class
00068      */
00069     protected ProcedureMapDetailDialog(Shell parentShell, String title, String proc, String procClass, RGB color) {
00070         super(parentShell);
00071         
00072         this.proc = proc;
00073         this.description = procClass;
00074         this.title = title;
00075         this.rgb = color;
00076     }
00077 
00078     /*
00079      * (non-Javadoc)
00080      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
00081      */
00082     protected Control createDialogArea(Composite parent) {
00083         Composite composite = (Composite) super.createDialogArea(parent);
00084 
00085         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
00086         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
00087         
00088         final Label lblIntro = new Label(composite, SWT.LEFT);
00089         lblIntro.setText("Please type the name of the procedure or the pattern of procedure name.\n"
00090                 + "Symbol * matches all characters, while symbol ? matches only one character.");
00091         GridDataFactory.swtDefaults().span(2, 1).applyTo(lblIntro);
00092         
00093         final Label lblProc = new Label(composite, SWT.LEFT);
00094         lblProc.setText("Procedure pattern: ");
00095         txtProc = new Text(composite, SWT.LEFT | SWT.SINGLE);
00096         txtProc.setText(proc);
00097         GridDataFactory.swtDefaults().hint(
00098                 this.convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
00099                 .grab(true, false).applyTo(txtProc);
00100         
00101         final Label lblClass = new Label(composite, SWT.LEFT);
00102         lblClass.setText("Description: ");
00103         txtClass = new Text(composite, SWT.LEFT | SWT.SINGLE);
00104         txtClass.setText(description);
00105         GridDataFactory.swtDefaults().hint(
00106                 this.convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
00107                 .grab(true, false).applyTo(txtClass);
00108         
00109         final Label lblColor = new Label(composite, SWT.LEFT);
00110         lblColor.setText("Color: ");
00111         final Button btnColor = new Button(composite, SWT.PUSH | SWT.FLAT);
00112         btnColor.computeSize(ColorTable.COLOR_ICON_SIZE, ColorTable.COLOR_ICON_SIZE);
00113         if (rgb == null) {
00114             rgb = getShell().getDisplay().getSystemColor(SWT.COLOR_BLACK).getRGB();
00115         }
00116         setButtonImage(btnColor, rgb);
00117         
00118         btnColor.addSelectionListener( new SelectionAdapter(){
00119             public void widgetSelected(SelectionEvent e) {
00120                 final Shell shell = ProcedureMapDetailDialog.this.getShell();
00121                 ColorDialog colorDlg = new ColorDialog(shell);
00122                 colorDlg.setRGB(rgb);
00123                 colorDlg.setText("Select color for " + ProcedureMapDetailDialog.this.description);
00124                 final RGB newRGB = colorDlg.open();
00125                 if (newRGB != null) {
00126                     rgb = newRGB;
00127                     setButtonImage(btnColor, rgb);
00128                 }
00129             }
00130         });
00131         
00132         return composite;
00133     }
00134 
00135     /***
00136      * set an image to a button given the color 
00137      * @param button
00138      * @param color
00139      */
00140     private void setButtonImage(Button button, RGB color) {
00141         Image oldImage = button.getImage();
00142         if (oldImage != null) {
00143             oldImage.dispose();
00144         }
00145         Image image = ColorTable.createImage(getShell().getDisplay(), color);
00146         button.setImage(image);
00147     }
00148     
00149     /*
00150      * (non-Javadoc)
00151      * 
00152      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
00153      */
00154     protected void configureShell(Shell shell) {
00155         super.configureShell(shell);
00156         if (title != null) {
00157             shell.setText(title);
00158         }
00159     }
00160 
00161     /*
00162      * (non-Javadoc)
00163      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
00164      */
00165     protected void okPressed() {
00166         proc = txtProc.getText();
00167         if (proc == null || proc.isEmpty()) {
00168             // we do not allow empty pattern
00169             
00170             MessageDialog.openError(getShell(), "Error", "Procedure pattern cannot be empty");
00171             Display display = getShell().getDisplay();
00172             txtProc.setBackground(display.getSystemColor(SWT.COLOR_RED));
00173             return;
00174         } else {
00175             // check the validity of the pattern
00176             
00177             try {
00178                 "Function".matches(proc.replace("*", ".*").replace("?", ".?"));
00179                 
00180                 // the pattern looks valid, ready to close the dialog
00181                 
00182                 description = txtClass.getText();
00183                 super.okPressed();
00184                 
00185             } catch (java.util.regex.PatternSyntaxException e) {
00186                 MessageDialog.openError(getShell(), "Error", "Pattern is not valid:\n" + e.getMessage());
00187             }
00188         }
00189     }
00190     
00191     /***
00192      * unit test
00193      * 
00194      * @param argv
00195      */
00196     static public void main(String argv[]) {
00197         Display display = new Display ();
00198         Shell shell = new Shell(display);
00199         shell.setLayout(new FillLayout());
00200         
00201         shell.open();
00202         
00203         ProcedureMapDetailDialog dlg = new ProcedureMapDetailDialog(shell, "edit", "procedure", "procedure-class", null);
00204 
00205         dlg.open();
00206         
00207         System.out.println("proc: " + dlg.proc + ", class: " + dlg.description);
00208         
00209         while (!shell.isDisposed()) {
00210             if (!display.readAndDispatch())
00211                 display.sleep();
00212         }
00213         
00214         display.dispose();
00215     }
00216 
00217 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1