summaryrefslogtreecommitdiff
path: root/platform/platform-api/src/com/intellij/openapi/ui/LabeledComponent.java
blob: a8a99eed6980c648d9628d4eec7fe1985cb2f40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.ui;

import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.PanelWithAnchor;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class LabeledComponent<Comp extends JComponent> extends JPanel implements PanelWithAnchor {
  private final JBLabel myLabel = new JBLabel();
  private Comp myComponent;
  private String myLabelConstraints = BorderLayout.NORTH;
  private JComponent myAnchor;

  public LabeledComponent() {
    super(new BorderLayout(UIUtil.DEFAULT_HGAP, 2));
    insertLabel();
  }

  @NotNull
  public static <Comp extends JComponent> LabeledComponent<Comp> create(@NotNull Comp component, @NotNull String text) {
    final LabeledComponent<Comp> labeledComponent = new LabeledComponent<Comp>();
    labeledComponent.setComponent(component);
    labeledComponent.setText(text);
    return labeledComponent;
  }

  private void insertLabel() {
    remove(myLabel);
    add(myLabel, myLabelConstraints);
    setAnchor(myLabel);
  }

  public void setText(String textWithMnemonic) {
    if (!StringUtil.endsWithChar(textWithMnemonic, ':')) textWithMnemonic += ":";
    TextWithMnemonic withMnemonic = TextWithMnemonic.fromTextWithMnemonic(textWithMnemonic);
    withMnemonic.setToLabel(myLabel);
  }

  public String getText() {
    String text = TextWithMnemonic.fromLabel(myLabel).getTextWithMnemonic();
    if (StringUtil.endsWithChar(text, ':')) return text.substring(0, text.length() - 1);
    return text;
  }

  public void setComponentClass(@NonNls String className) throws ClassNotFoundException, InstantiationException,
                                                                                           IllegalAccessException {
    if (className != null) {
      Class<Comp> aClass = (Class<Comp>)getClass().getClassLoader().loadClass(className);
      Comp component = aClass.newInstance();
      setComponent(component);
    }
    else {
      setComponent(null);
    }
  }

  public void setComponent(Comp component) {
    if (myComponent != null) remove(myComponent);
    myComponent = component;
    if (myComponent != null) {
      add(myComponent, BorderLayout.CENTER);
    }

    if (myComponent instanceof ComponentWithBrowseButton && !(myComponent instanceof TextFieldWithBrowseButton)) {
      myLabel.setLabelFor(((ComponentWithBrowseButton)myComponent).getChildComponent());
    } else myLabel.setLabelFor(myComponent);
  }

  public String getComponentClass() {
    if (myComponent == null) return null;
    return getComponent().getClass().getName();
  }

  public Comp getComponent() {
    return myComponent;
  }

  @Override
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if (myComponent != null) myComponent.setEnabled(enabled);
    myLabel.setEnabled(enabled);
  }

  public void setLabelLocation(@NonNls String borderConstrains) {
    String constrains = findBorderConstrains(borderConstrains);
    if (constrains == null || constrains.equals(myLabelConstraints)) return;
    myLabelConstraints = borderConstrains;
    insertLabel();
  }

  public String getLabelLocation() {
    return myLabelConstraints;
  }

  public Insets getLabelInsets() {
    return myLabel.getInsets();
  }

  public void setLabelInsets(Insets insets) {
    if (Comparing.equal(insets, getLabelInsets())) return;
    myLabel.setBorder(IdeBorderFactory.createEmptyBorder(insets));
  }

  private static final String[] LABEL_BORDER_CONSTRAINS = new String[]{BorderLayout.NORTH, BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST};

  private static String findBorderConstrains(String str) {
    for (String constrain : LABEL_BORDER_CONSTRAINS) {
      if (constrain.equals(str)) return constrain;
    }
    return null;
  }

  public String getRawText() {
    return myLabel.getText().replace("\u001B", "");
  }

  public JBLabel getLabel() {
    return myLabel;
  }

  @Override
  public JComponent getAnchor() {
    return myAnchor;
  }

  @Override
  public void setAnchor(@Nullable JComponent labelAnchor) {
    myAnchor = labelAnchor;
    myLabel.setAnchor(labelAnchor);
  }

  public static class TextWithMnemonic {
    private final String myText;
    private final int myMnemoniIndex;

    public TextWithMnemonic(String text, int mnemoniIndex) {
      myText = text;
      myMnemoniIndex = mnemoniIndex;
    }

    public void setToLabel(JLabel label) {
      label.setText(myText);
      if (myMnemoniIndex != -1) label.setDisplayedMnemonic(myText.charAt(myMnemoniIndex));
      else label.setDisplayedMnemonic(0);
      label.setDisplayedMnemonicIndex(myMnemoniIndex);
    }

    public String getTextWithMnemonic() {
      if (myMnemoniIndex == -1) return myText;
      return myText.substring(0, myMnemoniIndex) + "&" + myText.substring(myMnemoniIndex);
    }

    public static TextWithMnemonic fromTextWithMnemonic(String textWithMnemonic) {
      int mnemonicIndex = textWithMnemonic.indexOf('&');
      if (mnemonicIndex == -1) {
        return new TextWithMnemonic(textWithMnemonic, -1);
      }
      textWithMnemonic = textWithMnemonic.substring(0, mnemonicIndex) + textWithMnemonic.substring(mnemonicIndex + 1);
      return new TextWithMnemonic(textWithMnemonic, mnemonicIndex);
    }

    public static TextWithMnemonic fromLabel(JLabel label) {
      return new TextWithMnemonic(label.getText(), label.getDisplayedMnemonicIndex());
    }
  }
}