KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > NewCategoryDefinitionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.site;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.pde.internal.core.isite.*;
15 import org.eclipse.pde.internal.ui.*;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.layout.*;
19 import org.eclipse.swt.widgets.*;
20
21 public class NewCategoryDefinitionDialog extends BaseNewDialog {
22     private static final String JavaDoc KEY_TITLE = "NewCategoryDefinitionDialog.title"; //$NON-NLS-1$
23
private static final String JavaDoc KEY_NAME = "NewCategoryDefinitionDialog.name"; //$NON-NLS-1$
24
private static final String JavaDoc KEY_LABEL = "NewCategoryDefinitionDialog.label"; //$NON-NLS-1$
25
private static final String JavaDoc KEY_DESC = "NewCategoryDefinitionDialog.desc"; //$NON-NLS-1$
26
private static final String JavaDoc KEY_EMPTY = "NewCategoryDefinitionDialog.empty"; //$NON-NLS-1$
27
private Text nameText;
28     private Text labelText;
29     private Text descText;
30
31     public NewCategoryDefinitionDialog(
32         Shell shell,
33         ISiteModel siteModel,
34         ISiteCategoryDefinition def) {
35         super(shell, siteModel, def);
36     }
37
38     protected void createEntries(Composite container) {
39         GridData gd;
40         Label label = new Label(container, SWT.NULL);
41         label.setText(PDEPlugin.getResourceString(KEY_NAME));
42         nameText = new Text(container, SWT.SINGLE | SWT.BORDER);
43         gd = new GridData(GridData.FILL_HORIZONTAL);
44         nameText.setLayoutData(gd);
45
46         label = new Label(container, SWT.NULL);
47         label.setText(PDEPlugin.getResourceString(KEY_LABEL));
48         labelText = new Text(container, SWT.SINGLE | SWT.BORDER);
49         gd = new GridData(GridData.FILL_HORIZONTAL);
50         labelText.setLayoutData(gd);
51
52         label = new Label(container, SWT.NULL);
53         label.setText(PDEPlugin.getResourceString(KEY_DESC));
54         gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
55         label.setLayoutData(gd);
56         descText = new Text(container, SWT.MULTI | SWT.WRAP | SWT.BORDER);
57         gd = new GridData(GridData.FILL_BOTH);
58         gd.heightHint = 100;
59         gd.widthHint = 225;
60         descText.setLayoutData(gd);
61         if (getCategoryDefinition()==null) {
62             presetFields();
63         }
64     }
65     
66     private void presetFields() {
67     }
68     
69     private ISiteCategoryDefinition getCategoryDefinition() {
70         return (ISiteCategoryDefinition)getSiteObject();
71     }
72     
73     protected String JavaDoc getDialogTitle() {
74         return PDEPlugin.getResourceString(KEY_TITLE);
75     }
76     
77     protected String JavaDoc getHelpId() {
78         return IHelpContextIds.NEW_CATEGORY_DEF_DIALOG;
79     }
80     
81     protected String JavaDoc getEmptyErrorMessage() {
82         return PDEPlugin.getResourceString(KEY_EMPTY);
83     }
84
85     protected void hookListeners(ModifyListener modifyListener) {
86         nameText.addModifyListener(modifyListener);
87         labelText.addModifyListener(modifyListener);
88         descText.addModifyListener(modifyListener);
89     }
90
91     protected void initializeFields() {
92         super.initializeFields();
93         ISiteCategoryDefinition categoryDef = getCategoryDefinition();
94         setIfDefined(nameText, categoryDef.getName());
95         setIfDefined(labelText, categoryDef.getLabel());
96         setIfDefined(
97             descText,
98             categoryDef.getDescription() != null
99                 ? categoryDef.getDescription().getText()
100                 : null);
101     }
102
103     protected void dialogChanged() {
104         boolean edit = getCategoryDefinition()!=null;
105         IStatus status = null;
106         String JavaDoc name = nameText.getText();
107         if (name.length() == 0
108             || labelText.getText().length() == 0)
109             status = getEmptyErrorStatus();
110         else {
111             if (!edit && alreadyExists(name))
112                 status = createErrorStatus(PDEPlugin.getResourceString("NewCategoryDefinitionDialog.alreadyExists")); //$NON-NLS-1$
113
}
114         if (status==null)
115             status = getOKStatus();
116         updateStatus(status);
117     }
118     
119     private boolean alreadyExists(String JavaDoc name) {
120         ISiteCategoryDefinition [] defs = getSiteModel().getSite().getCategoryDefinitions();
121         for (int i=0; i<defs.length; i++) {
122             ISiteCategoryDefinition def = defs[i];
123             String JavaDoc dname = def.getName();
124             if (dname!=null && dname.equals(name))
125                 return true;
126         }
127         return false;
128     }
129
130     protected void execute() {
131         boolean add = false;
132         ISiteCategoryDefinition categoryDef = getCategoryDefinition();
133         ISiteModel siteModel = getSiteModel();
134         if (categoryDef == null) {
135             add = true;
136             categoryDef = siteModel.getFactory().createCategoryDefinition();
137         }
138         try {
139             categoryDef.setName(nameText.getText());
140             categoryDef.setLabel(labelText.getText());
141             String JavaDoc desc = descText.getText();
142             if (desc.length() > 0) {
143                 ISiteDescription description = categoryDef.getDescription();
144                 if (description == null)
145                     description =
146                         siteModel.getFactory().createDescription(categoryDef);
147                 description.setText(desc);
148                 categoryDef.setDescription(description);
149             } else {
150                 categoryDef.setDescription(null);
151             }
152             if (add) {
153                 siteModel.getSite().addCategoryDefinitions(
154                     new ISiteCategoryDefinition[] { categoryDef });
155             }
156         } catch (CoreException e) {
157             PDEPlugin.logException(e);
158         }
159     }
160 }
161
Popular Tags