From 91a34eb5d4adef4e010f762fada128d13a53f90b Mon Sep 17 00:00:00 2001 From: Sergey Vasiliev Date: Fri, 21 Jul 2006 16:30:30 +0400 Subject: [PATCH] added highligthing for empty combobox control --- .../src/com/intellij/util/xml/ui/ComboControl.java | 93 +++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/dom/openapi/src/com/intellij/util/xml/ui/ComboControl.java b/dom/openapi/src/com/intellij/util/xml/ui/ComboControl.java index 4a8af339e5..cc3aea3ed1 100644 --- a/dom/openapi/src/com/intellij/util/xml/ui/ComboControl.java +++ b/dom/openapi/src/com/intellij/util/xml/ui/ComboControl.java @@ -6,19 +6,30 @@ package com.intellij.util.xml.ui; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Factory; import com.intellij.openapi.util.Pair; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiManager; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.xml.*; +import com.intellij.util.xml.highlighting.DomElementAnnotationsManager; +import com.intellij.util.xml.highlighting.DomElementsProblemsHolder; +import com.intellij.util.xml.highlighting.DomElementProblemDescriptor; +import com.intellij.lang.annotation.HighlightSeverity; import org.jetbrains.annotations.NotNull; import javax.swing.*; +import javax.swing.plaf.basic.BasicComboBoxUI; +import javax.swing.plaf.basic.BasicComboBoxEditor; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import java.util.List; +import sun.swing.DefaultLookup; + /** * @author peter */ @@ -163,7 +174,8 @@ public class ComboControl extends BaseControl { } static JComboBox initComboBox(final JComboBox comboBox, final Condition validity) { - comboBox.setEditable(false); + comboBox.setEditable(true); + comboBox.setEditor(new MyComboBoxEditor()); comboBox.setPrototypeDisplayValue(Pair.create("A", null)); comboBox.setRenderer(new DefaultListCellRenderer() { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { @@ -222,6 +234,83 @@ public class ComboControl extends BaseControl { component.setEditable(true); } component.setSelectedItem(Pair.create(value, myIcons.get(value))); - component.setEditable(false); } + + + protected void updateComponent() { + final DomElement domElement = getDomElement(); + if (domElement == null || !domElement.isValid()) return; + + final JComboBox comboBox = getComponent(); + + final Project project = getProject(); + ApplicationManager.getApplication().invokeLater(new Runnable() { + public void run() { + if (!getDomWrapper().isValid()) return; + + final DomElement domElement = getDomElement(); + if (!project.isOpen() || domElement == null || !domElement.isValid()) return; + + final DomElementAnnotationsManager manager = DomElementAnnotationsManager.getInstance(project); + final DomElementsProblemsHolder holder = manager.getCachedProblemHolder(domElement); + final List errorProblems = holder.getProblems(domElement, true); + final List warningProblems = + holder.getProblems(domElement, true, true, HighlightSeverity.WARNING); + + Color background = getDefaultBackground(); + comboBox.setToolTipText(null); + + if (errorProblems.size() > 0) { + background = getErrorBackground(); + comboBox.setToolTipText(TooltipUtils.getTooltipText(errorProblems)); + } + else if (warningProblems.size() > 0) { + background = getWarningBackground(); + comboBox.setToolTipText(TooltipUtils.getTooltipText(warningProblems)); + } + + background = ((Pair)comboBox.getSelectedItem()).first.trim().length() > 0 ? getDefaultBackground() : background; + + comboBox.setBackground(background); + comboBox.getEditor().getEditorComponent().setBackground(background); + } + }); + + } + + private static class MyComboBoxEditor implements ComboBoxEditor { + private JLabel myLabel; + private Pair myCurrentItem; + + public MyComboBoxEditor() { + myLabel = new JLabel(); + + } + + public Component getEditorComponent() { + return myLabel; + } + + public void setItem(Object anObject) { + myCurrentItem = anObject instanceof Pair ? (Pair)anObject : EMPTY; + + myLabel.setText(myCurrentItem.first); + myLabel.setIcon(myCurrentItem.second); + } + + public Object getItem() { + return myCurrentItem; + } + + public void selectAll() { + } + + public void addActionListener(ActionListener l) { + + } + + public void removeActionListener(ActionListener l) { + } + } + } -- 2.11.4.GIT