Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1a55c7ff37af6d60dba72f141be2d234a9f74df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * 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:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060418   136335 joan@ca.ibm.com - Joan Haggarty
 * 20060418   136759 joan@ca.ibm.com - Joan Haggarty
 *******************************************************************************/
package org.eclipse.wst.command.internal.env.ui.widgets;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public abstract class SimpleDialog extends Dialog implements DialogDataEvents
{
  private   WidgetContributor widget_;
  private   WidgetDataEvents  dataEvents_;
  private   Listener          statusListener_;
  protected WizardPageManager pageManager_;  
  private Text messageText_;
  private boolean ok_;
  	
  public SimpleDialog( Shell shell, PageInfo pageInfo)
  {
	super(shell);
	widget_  = pageInfo.getWidgetFactory().create();
  	statusListener_ = new StatusListener();
  } 	 
  
  Composite parent_;
  public Control createDialogArea( Composite parent )
  {	
	  parent_ = parent;
	Composite control = (Composite) super.createDialogArea(parent);
	dataEvents_ = widget_.addControls( control, statusListener_ );
	
	//Text area to display validation messages
	messageText_ = new Text(parent, SWT.WRAP | SWT.MULTI | SWT.READ_ONLY);
    GridData griddata = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
    griddata.horizontalIndent = 10;
    messageText_.setLayoutData( griddata );    
    parent_.setFocus();
    
    //Call extenders override of the callSetters method to intialize any data in widget
    callSetters();   
    org.eclipse.jface.dialogs.Dialog.applyDialogFont(control);
    return control;
  }  
  
  //Override of Dialog.createContents so we can affect button enablement 
  protected Control createContents(Composite parent) {
	Control c = super.createContents(parent);
	//enable controls for initial entry to dialog
	validatePageToStatus();
	return c;
  }

  // Enable or disable ok button based on state
  private void enableOk(boolean state)
  {
	  Button okButton = getButton(IDialogConstants.OK_ID);
	  if (okButton != null)
	    okButton.setEnabled(state);
  }
  
  //Implement this method to call all setters on the dialog widget after the controls are created
  protected abstract void callSetters();
  
  public WidgetDataEvents getDataEvents()
  {
    return dataEvents_;
  }
  
  protected WidgetContributor getWidget()
  {
	  return widget_;
  }
  
  // sets message in message area above OK and Cancel buttons
  private void setMessage(String message)
  {  
	  if (message == null)
		  messageText_.setText("");	      
	  else		  
	      messageText_.setText(message);
      // force a resize of the parent composite and wrap of the text field if necessary
	  parent_.layout();  
  }
  	  
  
  
  public void validatePageToStatus()
  {
    IStatus status  = widget_.getStatus();    
        
    if( status != null )
    {
      String message = status.getMessage();
      int severity = status.getSeverity();
      
      // for error condition, disable ok and print message
      if( severity == IStatus.ERROR )
      { 
        if( message.length() == 0 )
        {
          setMessage( null);
        }
        else
        {
          setMessage(message);
        }
        ok_ = false;
      }
      // display warning and info messages but still enable OK
      else if (severity == IStatus.WARNING | severity == IStatus.INFO)
      {
        setMessage(message);
        ok_ = true;        
      }     
      // no message to display
      else  
      {
        setMessage( null);
        ok_ = true;
      }
    }
    // null status case
    else
    {
      setMessage(null);
      ok_ = true;
    }
    
    enableOk(ok_);    
  }
  
  private class StatusListener implements Listener
  {
	public void handleEvent( Event evt ) 
	{
      validatePageToStatus();
	}
  }
}  

Back to the top