Commit 42a4e126 authored by Roman Shevchenko's avatar Roman Shevchenko
Browse files

Cleanup (warnings; formatting)

parent 919aae22
Showing with 38 additions and 60 deletions
+38 -60
/*
* 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.ui;
import com.intellij.openapi.util.Comparing;
import com.intellij.BundleBase;
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.ArrayUtil;
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.*;
import java.util.Objects;
public class LabeledComponent<Comp extends JComponent> extends JPanel implements PanelWithAnchor {
private static final String[] LABEL_BORDER_CONSTRAINS = {BorderLayout.NORTH, BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST};
private final JBLabel myLabel = new JBLabel();
private Comp myComponent;
private String myLabelConstraints = BorderLayout.NORTH;
......@@ -45,8 +34,8 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
}
@NotNull
public static <Comp extends JComponent> LabeledComponent<Comp> create(@NotNull Comp component, @NotNull String text, @NonNls String labelConstraint) {
final LabeledComponent<Comp> labeledComponent = new LabeledComponent<>();
public static <Comp extends JComponent> LabeledComponent<Comp> create(@NotNull Comp component, @NotNull String text, String labelConstraint) {
LabeledComponent<Comp> labeledComponent = new LabeledComponent<>();
labeledComponent.setComponent(component);
labeledComponent.setText(text);
labeledComponent.setLabelLocation(labelConstraint);
......@@ -59,24 +48,22 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
setAnchor(myLabel);
}
public void setText(String textWithMnemonic) {
if (!StringUtil.isEmpty(textWithMnemonic) && !StringUtil.endsWithChar(textWithMnemonic, ':')) textWithMnemonic += ":";
TextWithMnemonic withMnemonic = TextWithMnemonic.fromTextWithMnemonic(textWithMnemonic);
withMnemonic.setToLabel(myLabel);
public void setText(String text) {
if (!StringUtil.isEmpty(text) && !StringUtil.endsWithChar(text, ':')) {
text += ':';
}
TextWithMnemonic.fromTextWithMnemonic(text).setToLabel(myLabel);
}
public String getText() {
String text = TextWithMnemonic.fromLabel(myLabel).getTextWithMnemonic();
if (StringUtil.endsWithChar(text, ':')) return text.substring(0, text.length() - 1);
return text;
return StringUtil.endsWithChar(text, ':') ? text.substring(0, text.length() - 1) : text;
}
public void setComponentClass(@NonNls String className) throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
public void setComponentClass(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
if (className != null) {
Class<Comp> aClass = (Class<Comp>)getClass().getClassLoader().loadClass(className);
Comp component = aClass.newInstance();
setComponent(component);
@SuppressWarnings("unchecked") Class<Comp> aClass = (Class<Comp>)getClass().getClassLoader().loadClass(className);
setComponent(aClass.newInstance());
}
else {
setComponent(null);
......@@ -89,15 +76,16 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
if (myComponent != null) {
add(myComponent, BorderLayout.CENTER);
}
if (myComponent instanceof ComponentWithBrowseButton && !(myComponent instanceof TextFieldWithBrowseButton)) {
myLabel.setLabelFor(((ComponentWithBrowseButton)myComponent).getChildComponent());
} else myLabel.setLabelFor(myComponent);
}
else {
myLabel.setLabelFor(myComponent);
}
}
public String getComponentClass() {
if (myComponent == null) return null;
return getComponent().getClass().getName();
return myComponent == null ? null : getComponent().getClass().getName();
}
public Comp getComponent() {
......@@ -111,11 +99,11 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
myLabel.setEnabled(enabled);
}
public void setLabelLocation(@NonNls String borderConstrains) {
String constrains = findBorderConstrains(borderConstrains);
if (constrains == null || constrains.equals(myLabelConstraints)) return;
myLabelConstraints = borderConstrains;
insertLabel();
public void setLabelLocation(String borderConstrains) {
if (ArrayUtil.indexOf(LABEL_BORDER_CONSTRAINS, borderConstrains) >= 0 && !borderConstrains.equals(myLabelConstraints)) {
myLabelConstraints = borderConstrains;
insertLabel();
}
}
public String getLabelLocation() {
......@@ -126,22 +114,15 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
return myLabel.getInsets();
}
@SuppressWarnings("unused")
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;
if (!Objects.equals(insets, getLabelInsets())) {
myLabel.setBorder(IdeBorderFactory.createEmptyBorder(insets));
}
return null;
}
public String getRawText() {
return myLabel.getText().replace("\u001B", "");
return myLabel.getText().replace(BundleBase.MNEMONIC_STRING, "");
}
public JBLabel getLabel() {
......@@ -176,21 +157,18 @@ public class LabeledComponent<Comp extends JComponent> extends JPanel implements
}
public String getTextWithMnemonic() {
if (myMnemonicIndex == -1) return myText;
return myText.substring(0, myMnemonicIndex) + "&" + myText.substring(myMnemonicIndex);
return myMnemonicIndex != -1 ? myText.substring(0, myMnemonicIndex) + '&' + myText.substring(myMnemonicIndex) : myText;
}
public static TextWithMnemonic fromTextWithMnemonic(String textWithMnemonic) {
int mnemonicIndex = UIUtil.getDisplayMnemonicIndex(textWithMnemonic);
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 fromTextWithMnemonic(String text) {
int mnemonicIndex = UIUtil.getDisplayMnemonicIndex(text);
return mnemonicIndex != -1
? new TextWithMnemonic(text.substring(0, mnemonicIndex) + text.substring(mnemonicIndex + 1), mnemonicIndex)
: new TextWithMnemonic(text, -1);
}
public static TextWithMnemonic fromLabel(JLabel label) {
return new TextWithMnemonic(label.getText(), label.getDisplayedMnemonicIndex());
}
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment