KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > properties > HibernatePropertyPage


1 package org.hibernate.eclipse.console.properties;
2
3 import org.eclipse.core.resources.ProjectScope;
4 import org.eclipse.core.runtime.CoreException;
5 import org.eclipse.core.runtime.NullProgressMonitor;
6 import org.eclipse.core.runtime.preferences.IScopeContext;
7 import org.eclipse.jdt.core.IJavaProject;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Combo;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.ui.dialogs.PropertyPage;
19 import org.hibernate.console.ConsoleConfiguration;
20 import org.hibernate.console.KnownConfigurations;
21 import org.hibernate.eclipse.console.HibernateConsolePlugin;
22 import org.hibernate.eclipse.console.utils.ProjectUtils;
23 import org.osgi.service.prefs.BackingStoreException;
24 import org.osgi.service.prefs.Preferences;
25
26 public class HibernatePropertyPage extends PropertyPage {
27
28     private static final int TEXT_FIELD_WIDTH = 50;
29     
30     Control[] settings;
31
32     private Button enableHibernate;
33
34     private Combo selectedConfiguration;
35     
36
37     /**
38      * Constructor for SamplePropertyPage.
39      */

40     public HibernatePropertyPage() {
41         super();
42     }
43
44     private void addFirstSection(Composite parent) {
45         Composite composite = createDefaultComposite(parent);
46
47         enableHibernate = new Button(composite, SWT.CHECK);
48         enableHibernate.setText("Enable Hibernate 3 support");
49         enableHibernate.addSelectionListener(new SelectionAdapter() {
50         
51             public void widgetSelected(SelectionEvent e) {
52                 boolean selection = enableHibernate.getSelection();
53                 enableSettings(selection);
54             }
55         });
56     }
57
58     private void addSeparator(Composite parent) {
59         Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
60         GridData gridData = new GridData();
61         gridData.horizontalAlignment = GridData.FILL;
62         gridData.grabExcessHorizontalSpace = true;
63         separator.setLayoutData(gridData);
64     }
65
66     private void addSecondSection(Composite parent) {
67         Composite settingsPart = createDefaultComposite(parent);
68
69         
70         // Label for owner field
71
Label ownerLabel = new Label(settingsPart, SWT.NONE);
72         ownerLabel.setText("Default Hibernate Console configuration:");
73
74         selectedConfiguration = new Combo(parent, SWT.DROP_DOWN);
75         GridData gd = new GridData();
76         gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
77         selectedConfiguration.setLayoutData(gd);
78
79         // Populate owner text field
80
ConsoleConfiguration[] configurations = KnownConfigurations.getInstance().getConfigurations();
81             for (int i = 0; i < configurations.length; i++) {
82                 ConsoleConfiguration configuration = configurations[i];
83                 selectedConfiguration.add(configuration.getName());
84             }
85         
86         settings = new Control[] { ownerLabel, selectedConfiguration };
87         
88     }
89
90     /**
91      * @see PreferencePage#createContents(Composite)
92      */

93     protected Control createContents(Composite parent) {
94         Composite composite = new Composite(parent, SWT.NONE);
95         GridLayout layout = new GridLayout();
96         composite.setLayout(layout);
97         GridData data = new GridData(GridData.FILL);
98         data.grabExcessHorizontalSpace = true;
99         composite.setLayoutData(data);
100
101         addFirstSection(composite);
102         addSeparator(composite);
103         addSecondSection(composite);
104         
105         loadValues();
106         enableSettings(enableHibernate.getSelection());
107         
108         return composite;
109     }
110
111     private Composite createDefaultComposite(Composite parent) {
112         Composite composite = new Composite(parent, SWT.NULL);
113         GridLayout layout = new GridLayout();
114         layout.numColumns = 2;
115         composite.setLayout(layout);
116
117         GridData data = new GridData();
118         data.verticalAlignment = GridData.FILL;
119         data.horizontalAlignment = GridData.FILL;
120         composite.setLayoutData(data);
121
122         return composite;
123     }
124
125     protected void performDefaults() {
126         // Populate the owner text field with the default value
127
//ownerText.setText(DEFAULT_OWNER);
128
}
129     
130     public void loadValues() {
131         IJavaProject prj = (IJavaProject) getElement();
132         IScopeContext scope = new ProjectScope(prj.getProject());
133         
134         Preferences node = scope.getNode("org.hibernate.eclipse.console");
135         
136         if(node!=null) {
137             enableHibernate.setSelection(node.getBoolean("hibernate3.enabled", false));
138             String JavaDoc cfg = node.get("default.configuration", prj.getProject().getName());
139             ConsoleConfiguration configuration = KnownConfigurations.getInstance().find(cfg);
140             if(configuration==null) {
141                 selectedConfiguration.setText("");
142             } else {
143                 selectedConfiguration.setText(cfg);
144             }
145         }
146         
147     }
148     public boolean performOk() {
149         IJavaProject prj = (IJavaProject) getElement();
150         IScopeContext scope = new ProjectScope(prj.getProject());
151         
152         Preferences node = scope.getNode("org.hibernate.eclipse.console");
153         
154         if(node!=null) {
155             node.putBoolean("hibernate3.enabled", enableHibernate.getSelection());
156             node.put("default.configuration", selectedConfiguration.getText());
157             try {
158                 node.flush();
159             } catch (BackingStoreException e) {
160                 HibernateConsolePlugin.getDefault().logErrorMessage("Could not save changes to preferences", e);
161                 return false;
162             }
163         } else {
164             return false;
165         }
166         
167         try {
168             if(enableHibernate.getSelection()) {
169                 ProjectUtils.addProjectNature(prj.getProject(), "org.hibernate.eclipse.console.hibernateNature", new NullProgressMonitor());
170             } else {
171                 ProjectUtils.removeProjectNature(prj.getProject(), "org.hibernate.eclipse.console.hibernateNature", new NullProgressMonitor());
172             }
173         } catch(CoreException ce) {
174             HibernateConsolePlugin.getDefault().logErrorMessage("Could not activate Hibernate nature on project " + prj.getProject().getName(), ce);
175             HibernateConsolePlugin.getDefault().log(ce.getStatus());
176         }
177         return true;
178     }
179
180     private void enableSettings(boolean selection) {
181         for (int i = 0; i < settings.length; i++) {
182             Control comp = settings[i];
183             comp.setEnabled(selection);
184         }
185     }
186
187 }
Popular Tags