KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > PDELabelUtility


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.pde.internal.ui.util;
13
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Label;
16
17 /**
18  * PDELabelUtility
19  *
20  */

21 public class PDELabelUtility {
22
23     /**
24      * Get the field label text from the label widget created before the
25      * control (assumption). Use this method when the control's label is
26      * variable or if the label's text is set elsewhere. Note: Hyperlink
27      * label text will not be detected.
28      * @param control
29      * @return
30      */

31     public static String JavaDoc getFieldLabel(Control control) {
32         // Note: Does not handle hyperlink labels
33

34         // Get the children of the control's parent
35
// The control itself should be in there
36
// Assuming the control's label is in there as well
37
Control[] controls = control.getParent().getChildren();
38         // Ensure has controls
39
if (controls.length == 0) {
40             return null;
41         }
42         // Me = control
43
// Track the index of myself
44
int myIndex = -1;
45         // Linearly search controls for myself
46
for (int i = 0; i < controls.length; i++) {
47             if (controls[i] == control) {
48                 // Found myself
49
myIndex = i;
50                 break;
51             }
52         }
53         // Ensure I was found and am not the first widget
54
if (myIndex <= 0) {
55             return null;
56         }
57         // Assume label is the control created before me
58
int labelIndex = myIndex - 1;
59         // Ensure the label index points to a label
60
if ((controls[labelIndex] instanceof Label) == false) {
61             return null;
62         }
63         // Get the label text
64
String JavaDoc labelText = ((Label)controls[labelIndex]).getText();
65         // Get rid of the trailing colon (if any)
66
int colonIndex = labelText.indexOf(':');
67         if (colonIndex >= 0) {
68             labelText = labelText.replaceAll(":", ""); //$NON-NLS-1$ //$NON-NLS-2$
69
}
70         // Get rid of mnemonics (if any)
71
int ampIndex = labelText.indexOf('&');
72         if (ampIndex >= 0) {
73             labelText = labelText.replaceAll("&", ""); //$NON-NLS-1$ //$NON-NLS-2$
74
}
75         
76         return labelText;
77     }
78     
79     /**
80      * @param qualification
81      * @param message
82      * @return
83      */

84     public static String JavaDoc qualifyMessage(String JavaDoc qualification, String JavaDoc message) {
85         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
86         buffer.append(qualification);
87         buffer.append(':');
88         buffer.append(' ');
89         buffer.append(message);
90         return buffer.toString();
91     }
92     
93 }
94
Popular Tags