KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > DialogSettingsHelper


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

11  
12 package org.eclipse.ant.internal.ui.preferences;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.jface.dialogs.IDialogSettings;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Shell;
18
19 /**
20  * Helper class for dealing with setting and restoring dialog settings.
21  */

22 public class DialogSettingsHelper {
23     public static final String JavaDoc DIALOG_ORIGIN_X = AntUIPlugin.PI_ANTUI + ".ATTR_ANT_DIALOG_X_ORIGIN"; //$NON-NLS-1$
24
public static final String JavaDoc DIALOG_ORIGIN_Y = AntUIPlugin.PI_ANTUI + ".ATTR_ANT_DIALOG_Y_ORIGIN"; //$NON-NLS-1$
25
public static final String JavaDoc DIALOG_WIDTH = AntUIPlugin.PI_ANTUI + ".ATTR_ANT_DIALOG_WIDTH"; //$NON-NLS-1$
26
public static final String JavaDoc DIALOG_HEIGHT = AntUIPlugin.PI_ANTUI + ".ATTR_ANT_DIALOG_HEIGHT"; //$NON-NLS-1$
27

28     /**
29      * Persists the location and dimensions of the shell in the
30      * Ant UI Plugin dialog settings under the provided dialog settings section name
31      *
32      * @param shell The shell whose geometry is to be stored
33      * @param dialogSettingsSectionName The name of the dialog settings section
34      */

35     public static void persistShellGeometry(Shell shell, String JavaDoc dialogSettingsSectionName) {
36         Point shellLocation = shell.getLocation();
37         Point shellSize = shell.getSize();
38         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
39         settings.put(DIALOG_ORIGIN_X, shellLocation.x);
40         settings.put(DIALOG_ORIGIN_Y, shellLocation.y);
41         settings.put(DIALOG_WIDTH, shellSize.x);
42         settings.put(DIALOG_HEIGHT, shellSize.y);
43     }
44     
45     private static IDialogSettings getDialogSettings(String JavaDoc dialogSettingsSectionName) {
46         IDialogSettings settings = AntUIPlugin.getDefault().getDialogSettings();
47         IDialogSettings section = settings.getSection(dialogSettingsSectionName);
48         if (section == null) {
49             section = settings.addNewSection(dialogSettingsSectionName);
50         }
51         return section;
52     }
53     
54     /**
55      * Returns the initial size which is the larger of the <code>initialSize</code> or
56      * the size persisted in the Ant UI Plugin dialog settings under the provided dialog setttings section name.
57      * If no size is persisted in the settings, the <code>initialSize</code> is returned.
58      *
59      * @param initialSize The initialSize to compare against
60      * @param dialogSettingsSectionName The name of the dialog settings section
61      * @return the initial size
62      */

63     public static Point getInitialSize(String JavaDoc dialogSettingsSectionName, Point initialSize) {
64         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
65         try {
66             int x, y;
67             x = settings.getInt(DIALOG_WIDTH);
68             y = settings.getInt(DIALOG_HEIGHT);
69             return new Point(Math.max(x, initialSize.x), Math.max(y, initialSize.y));
70         } catch (NumberFormatException JavaDoc e) {
71         }
72         return initialSize;
73     }
74     
75     /**
76      * Returns the initial location which is persisted in the Ant UI Plugin dialog settings
77      * under the provided dialog setttings section name.
78      * If location is not persisted in the settings, the <code>null</code> is returned.
79      *
80      * @param dialogSettingsSectionName The name of the dialog settings section
81      * @return The initial location or <code>null</code>
82      */

83     public static Point getInitialLocation(String JavaDoc dialogSettingsSectionName) {
84         IDialogSettings settings = getDialogSettings(dialogSettingsSectionName);
85         try {
86             int x= settings.getInt(DIALOG_ORIGIN_X);
87             int y= settings.getInt(DIALOG_ORIGIN_Y);
88             return new Point(x,y);
89         } catch (NumberFormatException JavaDoc e) {
90         }
91         return null;
92     }
93 }
94
Popular Tags