1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer; 27 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 import org.netbeans.modules.xml.schema.model.Documentation; 31 import org.openide.util.NbBundle; 32 33 37 public class DocumentationPanel extends javax.swing.JPanel 38 { 39 static final long serialVersionUID = 1L; 40 public static final String STATE_PROPERTY = "state"; 41 42 private transient Documentation documentation; 43 private transient DocumentListener contentListener; 44 private transient AnnotationCustomizer owner; 45 private transient State state; 46 49 public DocumentationPanel(AnnotationCustomizer owner, 50 Documentation documentation, boolean isAdded) 51 { 52 this.owner = owner; 53 this.documentation = documentation; 54 initComponents(); 55 initUI(); 56 setState(State.UNMODIFIED); 57 addPropertyChangeListener(STATE_PROPERTY,owner); 58 if(isAdded) 59 updateState(State.ADDED); 60 } 61 62 private void initUI() 63 { 64 innerPanel.setVisible(false); 65 if(contentListener == null) 66 { 67 contentListener = new DocumentListener () { 68 public void changedUpdate(DocumentEvent e) 69 { 70 updateState(State.MODIFIED); 71 } 72 public void insertUpdate(DocumentEvent e) 73 { 74 updateState(State.MODIFIED); 75 } 76 public void removeUpdate(DocumentEvent e) 77 { 78 updateState(State.MODIFIED); 79 } 80 }; 81 } else 82 { 83 contentEditorPane.getDocument(). 84 removeDocumentListener(contentListener); 85 } 86 contentEditorPane.setText(getDocumentation().getContentFragment()); 87 contentEditorPane.getDocument(). 88 addDocumentListener(contentListener); 89 } 90 91 public Documentation getDocumentation() 92 { 93 return documentation; 94 } 95 96 private boolean isModified() 97 { 98 return !getContent().equals(getDocumentation().getContentFragment()); 99 } 100 101 public String getContent() 102 { 103 return contentEditorPane.getText(); 104 } 105 106 private String getTitleButtonText() 107 { 108 String content = ""; 109 if(!titleButton.isSelected()) 110 { 111 content = getDocumentation().getContentFragment().trim(); 112 if(content.length()==0) 113 { 114 content = NbBundle.getMessage(DocumentationPanel.class, 115 "LBL_DocumentationEmptyContent"); 116 } 117 else if (content.length()>20) 118 { 119 content = content.substring(0,20); 120 content = content.replaceAll("<","<"); 121 } 122 } 123 return NbBundle.getMessage(DocumentationPanel.class, 124 "LBL_Documentation", content); 125 } 126 127 public State getState() 128 { 129 return state; 130 } 131 132 void setState(State state) 133 { 134 this.state = state; 135 } 136 137 private void updateState(final State state) 138 { 139 State newState = state; 140 State currentState = getState(); 141 if(currentState==State.ADDED && newState!= State.REMOVED) return; 143 if(newState==State.MODIFIED && !isModified()) 144 { 145 newState = State.UNMODIFIED; 147 } 148 setState(newState); 149 firePropertyChange(STATE_PROPERTY,currentState,newState); 150 } 151 152 public enum State {ADDED,REMOVED,MODIFIED,UNMODIFIED} 153 154 159 private void initComponents() 161 { 162 titleButton = new javax.swing.JButton (); 163 innerPanel = new javax.swing.JPanel (); 164 removeButton = new javax.swing.JButton (); 165 contentScrollPane = new javax.swing.JScrollPane (); 166 contentEditorPane = new javax.swing.JEditorPane () 167 { 168 static final long serialVersionUID = 1L; 169 protected void processMouseEvent(java.awt.event.MouseEvent e) 170 { 171 if(e.getButton()==java.awt.event.MouseEvent.BUTTON3) 172 { 173 e.consume(); 174 return; 175 } 176 super.processMouseEvent(e); 177 } 178 }; 179 180 setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); 181 titleButton.setIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/xml/schema/ui/nodes/resources/plus.gif"))); 182 titleButton.setText(getTitleButtonText()); 183 titleButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); 184 titleButton.setBorderPainted(false); 185 titleButton.setContentAreaFilled(false); 186 titleButton.setFocusPainted(false); 187 titleButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); 188 titleButton.setSelectedIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/xml/schema/ui/nodes/resources/minus.gif"))); 189 titleButton.addActionListener(new java.awt.event.ActionListener () 190 { 191 public void actionPerformed(java.awt.event.ActionEvent evt) 192 { 193 manageInnerPanel(evt); 194 } 195 }); 196 197 innerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color (204, 204, 204))); 198 removeButton.setText(org.openide.util.NbBundle.getMessage(DocumentationPanel.class, "LBL_Remove")); 199 removeButton.addActionListener(new java.awt.event.ActionListener () 200 { 201 public void actionPerformed(java.awt.event.ActionEvent evt) 202 { 203 removeButtonActionPerformed(evt); 204 } 205 }); 206 207 contentScrollPane.setBorder(null); 208 contentEditorPane.setContentType("text/xml"); 209 contentScrollPane.setViewportView(contentEditorPane); 210 211 org.jdesktop.layout.GroupLayout innerPanelLayout = new org.jdesktop.layout.GroupLayout(innerPanel); 212 innerPanel.setLayout(innerPanelLayout); 213 innerPanelLayout.setHorizontalGroup( 214 innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 215 .add(innerPanelLayout.createSequentialGroup() 216 .addContainerGap() 217 .add(innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 218 .add(org.jdesktop.layout.GroupLayout.TRAILING, removeButton) 219 .add(contentScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)) 220 .addContainerGap()) 221 ); 222 innerPanelLayout.setVerticalGroup( 223 innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 224 .add(org.jdesktop.layout.GroupLayout.TRAILING, innerPanelLayout.createSequentialGroup() 225 .add(contentScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE) 226 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 227 .add(removeButton) 228 .addContainerGap()) 229 ); 230 231 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 232 this.setLayout(layout); 233 layout.setHorizontalGroup( 234 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 235 .add(innerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 236 .add(titleButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE) 237 ); 238 layout.setVerticalGroup( 239 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 240 .add(layout.createSequentialGroup() 241 .add(titleButton) 242 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 243 .add(innerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 244 ); 245 } 247 private void manageInnerPanel(java.awt.event.ActionEvent evt) { boolean expand = !titleButton.isSelected(); 250 setExpanded(expand); 251 } 253 void setExpanded(final boolean expand) 254 { 255 titleButton.setSelected(expand); 256 titleButton.setText(getTitleButtonText()); 257 innerPanel.setVisible(expand); 258 } 259 260 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { updateState(State.REMOVED); 263 } 265 266 private javax.swing.JEditorPane contentEditorPane; 268 private javax.swing.JScrollPane contentScrollPane; 269 private javax.swing.JPanel innerPanel; 270 private javax.swing.JButton removeButton; 271 private javax.swing.JButton titleButton; 272 274 } 275 | Popular Tags |