1 19 20 package org.netbeans.editor.ext; 21 22 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.MouseAdapter ; 30 import java.awt.event.MouseListener ; 31 import java.awt.event.MouseEvent ; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 import javax.swing.Action ; 38 import javax.swing.ActionMap ; 39 import javax.swing.BorderFactory ; 40 import javax.swing.InputMap ; 41 import javax.swing.JComponent ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.JLabel ; 44 import javax.swing.JList ; 45 import javax.swing.KeyStroke ; 46 import javax.swing.SwingUtilities ; 47 import javax.swing.text.JTextComponent ; 48 49 import org.netbeans.editor.BaseKit; 50 import org.netbeans.editor.Settings; 51 import org.netbeans.editor.SettingsUtil; 52 import org.netbeans.editor.SettingsChangeListener; 53 import org.netbeans.editor.SettingsChangeEvent; 54 import org.netbeans.editor.Utilities; 55 56 57 64 65 public class ScrollCompletionPane extends JScrollPane implements ExtCompletionPane, 66 PropertyChangeListener , SettingsChangeListener { 67 68 private ExtEditorUI extEditorUI; 69 70 private JComponent view; 71 72 private JLabel topLabel; 73 74 private Dimension minSize; 75 76 private Dimension maxSize; 77 78 private ViewMouseListener viewMouseL; 79 80 private Dimension scrollBarSize; 81 82 private Dimension minSizeDefault; 83 84 public ScrollCompletionPane(ExtEditorUI extEditorUI) { 85 this.extEditorUI = extEditorUI; 86 87 Dimension smallSize = getPreferredSize(); 89 setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS); 90 setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS); 91 scrollBarSize = getPreferredSize(); 92 scrollBarSize.width -= smallSize.width; 93 scrollBarSize.height -= smallSize.height; 94 setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED); 95 setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED); 96 97 super.setVisible(false); 99 100 installTitleComponent(); 102 103 CompletionView completionView = extEditorUI.getCompletion().getView(); 105 if (completionView instanceof JComponent ) { 106 view = (JComponent )completionView; 107 setViewportView(view); 108 } 109 110 getViewport().setMinimumSize(new Dimension (4,4)); 112 113 Settings.addSettingsChangeListener(this); 114 115 viewMouseL = new ViewMouseListener(); 116 synchronized (extEditorUI.getComponentLock()) { 117 JTextComponent component = extEditorUI.getComponent(); 119 if (component != null) { 120 propertyChange(new PropertyChangeEvent (extEditorUI, 121 ExtEditorUI.COMPONENT_PROPERTY, null, component)); 122 } 123 124 extEditorUI.addPropertyChangeListener(this); 125 } 126 127 putClientProperty ("HelpID", ScrollCompletionPane.class.getName ()); } 129 130 131 public void settingsChange(SettingsChangeEvent evt) { 132 Class kitClass = Utilities.getKitClass(extEditorUI.getComponent()); 133 134 if (kitClass != null) { 135 minSize = (Dimension )SettingsUtil.getValue(kitClass, 136 ExtSettingsNames.COMPLETION_PANE_MIN_SIZE, 137 ExtSettingsDefaults.defaultCompletionPaneMinSize); 138 minSizeDefault = new Dimension (minSize); 139 setMinimumSize(minSize); 140 141 maxSize = (Dimension )SettingsUtil.getValue(kitClass, 142 ExtSettingsNames.COMPLETION_PANE_MAX_SIZE, 143 ExtSettingsDefaults.defaultCompletionPaneMaxSize); 144 setMaximumSize(maxSize); 145 146 } 147 } 148 149 150 public void propertyChange(PropertyChangeEvent evt) { 151 String propName = evt.getPropertyName(); 152 153 if (ExtEditorUI.COMPONENT_PROPERTY.equals(propName)) { 154 if (evt.getNewValue() != null) { 156 settingsChange(null); 157 158 if (view != null) { 159 view.addMouseListener(viewMouseL); 161 } 162 163 164 } else { 166 if (view != null) { 167 view.removeMouseListener(viewMouseL); 169 } 170 } 171 } 172 } 173 174 public void setVisible(boolean visible){ 175 if (view instanceof JList ) { 177 JList listView = (JList )view; 178 listView.ensureIndexIsVisible(listView.getSelectedIndex()); 179 } 180 181 super.setVisible(visible); 182 } 183 184 public void refresh() { 185 if (view instanceof JList ) { 186 JList listView = (JList )view; 187 listView.ensureIndexIsVisible(listView.getSelectedIndex()); 188 } 189 190 SwingUtilities.invokeLater( new Runnable () { 192 public void run() { 193 if (isShowing()) { revalidate(); 196 } 197 } 198 } 199 ); 200 } 201 202 203 public void setTitle(String title) { 204 topLabel.setText(title); 205 } 206 207 208 protected void installTitleComponent() { 209 topLabel = new JLabel (); 210 topLabel.setForeground(Color.blue); 211 topLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); 212 setColumnHeaderView(topLabel); 213 } 214 215 protected Dimension getTitleComponentPreferredSize() { 216 return topLabel.getPreferredSize(); 217 } 218 219 public void setSize(int width, int height){ 220 int maxWidth = width; 221 int maxHeight = height; 222 223 minSize.width = minSizeDefault.width; 224 minSize.height = minSizeDefault.height; 225 setMinimumSize(minSize); 226 227 Dimension ps = getPreferredSize(); 228 229 231 ps.width += scrollBarSize.width; 232 ps.width = Math.max(Math.max(ps.width, minSize.width), 233 getTitleComponentPreferredSize().width); 234 235 maxWidth = Math.min(maxWidth, maxSize.width); 236 maxHeight = Math.min(maxHeight, maxSize.height); 237 boolean displayHorizontalScrollbar = (ps.width-scrollBarSize.width)>maxWidth; 238 239 if (ps.width > maxWidth) { 240 ps.width = maxWidth; 241 if (displayHorizontalScrollbar){ 242 ps.height += scrollBarSize.height; minSize.height += scrollBarSize.height; 244 setMinimumSize(minSize); 245 } 246 247 } 248 249 ps.height = Math.min(Math.max(ps.height, minSize.height), maxHeight); 250 super.setSize(ps.width, ps.height); 251 } 252 253 public void setSize(Dimension d){ 254 setSize(d.width, d.height); 255 } 256 257 public JComponent getComponent() { 258 return this; 259 } 260 261 class ViewMouseListener extends MouseAdapter { 262 263 public void mouseClicked(MouseEvent evt) { 264 if (SwingUtilities.isLeftMouseButton(evt)) { 265 JTextComponent component = extEditorUI.getComponent(); 266 if( component != null && evt.getClickCount() == 2 ) { 267 JDCPopupPanel jdc = ExtUtilities.getJDCPopupPanel(component); 268 if (jdc != null) { 269 Action a = jdc.getActionMap().get(JDCPopupPanel.COMPLETION_SUBSTITUTE_TEXT); 270 if (a != null) { 271 a.actionPerformed(new ActionEvent (component, ActionEvent.ACTION_PERFORMED, "")); } 273 } 274 } 275 } 276 } 277 } 278 279 } 280 | Popular Tags |