KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > preferences > IgnorePreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.team.internal.ui.preferences;
12
13  
14 import org.eclipse.jface.dialogs.*;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.preference.PreferencePage;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.osgi.util.TextProcessor;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.team.core.IIgnoreInfo;
25 import org.eclipse.team.core.Team;
26 import org.eclipse.team.internal.ui.*;
27 import org.eclipse.team.ui.TeamUI;
28 import org.eclipse.ui.*;
29 public class IgnorePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
30     private Table ignoreTable;
31     private Button addButton;
32     private Button removeButton;
33     public void init(IWorkbench workbench) {
34         setDescription(TeamUIMessages.IgnorePreferencePage_description);
35     }
36     
37     /**
38      * Creates preference page controls on demand.
39      *
40      * @param ancestor the parent for the preference page
41      */

42     protected Control createContents(Composite ancestor) {
43         
44         Composite parent = new Composite(ancestor, SWT.NULL);
45         GridLayout layout = new GridLayout();
46         layout.marginWidth = 0;
47         layout.marginHeight = 0;
48         layout.numColumns = 2;
49         parent.setLayout(layout);
50         GridData data = new GridData();
51         data.verticalAlignment = GridData.FILL;
52         data.horizontalAlignment = GridData.FILL;
53         parent.setLayoutData(data);
54         
55         Label l1 = new Label(parent, SWT.NULL);
56         l1.setText(TeamUIMessages.IgnorePreferencePage_ignorePatterns);
57         data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
58         data.horizontalSpan = 2;
59         l1.setLayoutData(data);
60         
61         ignoreTable = new Table(parent, SWT.CHECK | SWT.BORDER);
62         GridData gd = new GridData(GridData.FILL_BOTH);
63         //gd.widthHint = convertWidthInCharsToPixels(30);
64
gd.heightHint = 300;
65         ignoreTable.setLayoutData(gd);
66         ignoreTable.addListener(SWT.Selection, new Listener() {
67             public void handleEvent(Event e) {
68                 handleSelection();
69             }
70         });
71         
72         Composite buttons = new Composite(parent, SWT.NULL);
73         buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
74         layout = new GridLayout();
75         layout.marginHeight = 0;
76         layout.marginWidth = 0;
77         buttons.setLayout(layout);
78         
79         addButton = new Button(buttons, SWT.PUSH);
80         addButton.setText(TeamUIMessages.IgnorePreferencePage_add);
81         addButton.addListener(SWT.Selection, new Listener() {
82             public void handleEvent(Event e) {
83                 addIgnore();
84             }
85         });
86         
87         removeButton = new Button(buttons, SWT.PUSH);
88         removeButton.setText(TeamUIMessages.IgnorePreferencePage_remove);
89         removeButton.setEnabled(false);
90         removeButton.addListener(SWT.Selection, new Listener() {
91             public void handleEvent(Event e) {
92                 removeIgnore();
93             }
94         });
95         fillTable(Team.getAllIgnores());
96         Dialog.applyDialogFont(ancestor);
97         setButtonLayoutData(addButton);
98         setButtonLayoutData(removeButton);
99         
100         // set F1 help
101
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.IGNORE_PREFERENCE_PAGE);
102         
103         return parent;
104     }
105     /**
106      * Do anything necessary because the OK button has been pressed.
107      *
108      * @return whether it is okay to close the preference page
109      */

110     public boolean performOk() {
111         int count = ignoreTable.getItemCount();
112         String JavaDoc[] patterns = new String JavaDoc[count];
113         boolean[] enabled = new boolean[count];
114         TableItem[] items = ignoreTable.getItems();
115         for (int i = 0; i < count; i++) {
116             patterns[i] = items[i].getText();
117             enabled[i] = items[i].getChecked();
118         }
119         Team.setAllIgnores(patterns, enabled);
120         TeamUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, TeamUI.GLOBAL_IGNORES_CHANGED, null, null));
121         return true;
122     }
123     
124     protected void performDefaults() {
125         super.performDefaults();
126         ignoreTable.removeAll();
127         IIgnoreInfo[] ignore = Team.getDefaultIgnores();
128         fillTable(ignore);
129     }
130     
131     /**
132      * @param ignore
133      */

134     private void fillTable(IIgnoreInfo[] ignore) {
135         for (int i = 0; i < ignore.length; i++) {
136             IIgnoreInfo info = ignore[i];
137             TableItem item = new TableItem(ignoreTable, SWT.NONE);
138             item.setText(TextProcessor.process(info.getPattern(), ".*")); //$NON-NLS-1$
139
item.setChecked(info.getEnabled());
140         }
141     }
142
143     private void addIgnore() {
144         InputDialog dialog = new InputDialog(getShell(), TeamUIMessages.IgnorePreferencePage_enterPatternShort, TeamUIMessages.IgnorePreferencePage_enterPatternLong, null, null); //
145
dialog.open();
146         if (dialog.getReturnCode() != Window.OK) return;
147         String JavaDoc pattern = dialog.getValue();
148         if (pattern.equals("")) return; //$NON-NLS-1$
149
// Check if the item already exists
150
TableItem[] items = ignoreTable.getItems();
151         for (int i = 0; i < items.length; i++) {
152             if (items[i].getText().equals(pattern)) {
153                 MessageDialog.openWarning(getShell(), TeamUIMessages.IgnorePreferencePage_patternExistsShort, TeamUIMessages.IgnorePreferencePage_patternExistsLong); //
154
return;
155             }
156         }
157         TableItem item = new TableItem(ignoreTable, SWT.NONE);
158         item.setText(TextProcessor.process(pattern, ".*")); //$NON-NLS-1$
159
item.setChecked(true);
160     }
161     
162     private void removeIgnore() {
163         int[] selection = ignoreTable.getSelectionIndices();
164         ignoreTable.remove(selection);
165     }
166     private void handleSelection() {
167         if (ignoreTable.getSelectionCount() > 0) {
168             removeButton.setEnabled(true);
169         } else {
170             removeButton.setEnabled(false);
171         }
172     }
173 }
174
Popular Tags