View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.connect.ui.parts;
17  
18  import org.argeo.eclipse.ui.EclipseUiUtils;
19  import org.eclipse.jface.dialogs.MessageDialog;
20  import org.eclipse.jface.dialogs.TrayDialog;
21  import org.eclipse.swt.SWT;
22  import org.eclipse.swt.events.ModifyEvent;
23  import org.eclipse.swt.events.ModifyListener;
24  import org.eclipse.swt.graphics.Point;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.layout.GridLayout;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Control;
29  import org.eclipse.swt.widgets.Label;
30  import org.eclipse.swt.widgets.Shell;
31  import org.eclipse.swt.widgets.Text;
32  
33  /**
34   * Generic dialog with 2 texts fields to ask the end user for a title and a
35   * description
36   */
37  public class AskTitleDescriptionDialog extends TrayDialog {
38  	private static final long serialVersionUID = -1965274702044063832L;
39  	// The various field
40  	private Text titleTxt;
41  	private Text descTxt;
42  
43  	private String title;
44  	private String desc;
45  
46  	private final String windowTitle;
47  
48  	/**
49  	 * @param parentShell
50  	 * @param windowTitle
51  	 */
52  	public AskTitleDescriptionDialog(Shell parentShell, String windowTitle) {
53  		super(parentShell);
54  		this.windowTitle = windowTitle;
55  	}
56  
57  	public AskTitleDescriptionDialog(Shell parentShell, String windowTitle, String defaultTitle, String defaultDesc) {
58  		super(parentShell);
59  		this.windowTitle = windowTitle;
60  		title = defaultTitle;
61  		desc = defaultDesc;
62  	}
63  
64  	protected Control createDialogArea(Composite parent) {
65  		// MAIN LAYOUT
66  		Composite dialogarea = (Composite) super.createDialogArea(parent);
67  		dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
68  		dialogarea.setLayout(new GridLayout(2, false));
69  
70  		// Title
71  		titleTxt = createLT(dialogarea, "Title");
72  		if (EclipseUiUtils.notEmpty(title))
73  			titleTxt.setText(title);
74  		titleTxt.setMessage("Please provide a title");
75  		titleTxt.addModifyListener(new ModifyListener() {
76  			private static final long serialVersionUID = 1L;
77  
78  			@Override
79  			public void modifyText(ModifyEvent event) {
80  				title = titleTxt.getText();
81  			}
82  		});
83  
84  		// Description
85  		Label lbl = new Label(dialogarea, SWT.RIGHT);
86  		lbl.setText("Description");
87  		lbl.setFont(EclipseUiUtils.getBoldFont(dialogarea));
88  		lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
89  		descTxt = new Text(dialogarea, SWT.MULTI | SWT.WRAP | SWT.BORDER);
90  		descTxt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91  		if (EclipseUiUtils.notEmpty(desc))
92  			descTxt.setText(desc);
93  		descTxt.setMessage("... and optionally a description");
94  		descTxt.addModifyListener(new ModifyListener() {
95  			private static final long serialVersionUID = 1L;
96  
97  			@Override
98  			public void modifyText(ModifyEvent event) {
99  				desc = descTxt.getText();
100 			}
101 		});
102 		parent.pack();
103 		return dialogarea;
104 	}
105 
106 	public String getTitle() {
107 		return title;
108 	}
109 
110 	public String getDescription() {
111 		return desc;
112 	}
113 
114 	/**
115 	 * Override this to perform the real addition
116 	 * 
117 	 * @return
118 	 */
119 	protected boolean performFinish() {
120 		// Sanity check
121 		String msg = null;
122 
123 		if (EclipseUiUtils.isEmpty(title) || title.length() < 2)
124 			msg = "Please enter a title that is at list two (2) valid charaters long.";
125 		if (msg != null) {
126 			MessageDialog.openError(getShell(), "Non valid information", msg);
127 			return false;
128 		}
129 
130 		return true;
131 	}
132 
133 	// This dialog life cycle
134 	@Override
135 	protected void okPressed() {
136 		if (performFinish())
137 			super.okPressed();
138 	}
139 
140 	protected Point getInitialSize() {
141 		return new Point(400, 250);
142 	}
143 
144 	protected void configureShell(Shell shell) {
145 		super.configureShell(shell);
146 		shell.setText(windowTitle);
147 	}
148 
149 	// Specific widgets management
150 	/** Creates label and text. */
151 	protected Text createLT(Composite parent, String label) {
152 		Label lbl = new Label(parent, SWT.RIGHT);
153 		lbl.setText(label);
154 		lbl.setFont(EclipseUiUtils.getBoldFont(parent));
155 		lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
156 		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
157 		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
158 		return text;
159 	}
160 }