1 19 package org.netbeans.modules.subversion.ui.history; 20 21 import org.openide.nodes.*; 22 import org.openide.util.lookup.Lookups; 23 import org.openide.util.NbBundle; 24 import org.openide.util.HelpCtx; 25 import org.openide.util.actions.SystemAction; 26 import org.openide.util.actions.NodeAction; 27 import org.openide.ErrorManager; 28 29 import javax.swing.*; 30 import java.beans.PropertyEditor ; 31 import java.beans.PropertyEditorSupport ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.Graphics ; 35 import java.awt.Rectangle ; 36 import java.text.DateFormat ; 37 import java.util.*; 38 39 44 class RevisionNode extends AbstractNode { 45 46 static final String COLUMN_NAME_NAME = "name"; static final String COLUMN_NAME_DATE = "date"; static final String COLUMN_NAME_USERNAME = "username"; static final String COLUMN_NAME_MESSAGE = "message"; 51 private RepositoryRevision.Event event; 52 private RepositoryRevision container; 53 private String path; 54 55 public RevisionNode(RepositoryRevision container, SearchHistoryPanel master) { 56 super(new RevisionNodeChildren(container, master), Lookups.fixed(master, container)); 57 this.container = container; 58 this.event = null; 59 this.path = null; 60 setName(container.getLog().getRevision().getNumber() + 61 NbBundle.getMessage(RevisionNode.class, "LBL_NumberOfChangedPaths", container.getLog().getChangedPaths().length)); 62 initProperties(); 63 } 64 65 public RevisionNode(RepositoryRevision.Event revision, SearchHistoryPanel master) { 66 super(Children.LEAF, Lookups.fixed(master, revision)); 67 this.path = revision.getChangedPath().getPath(); 68 this.event = revision; 69 setName(revision.getName()); 70 initProperties(); 71 } 72 73 RepositoryRevision.Event getRevision() { 74 return event; 75 } 76 77 RepositoryRevision getContainer() { 78 return container; 79 } 80 81 RepositoryRevision.Event getEvent() { 82 return event; 83 } 84 85 public String getShortDescription() { 86 return path; 87 } 88 89 public Action[] getActions(boolean context) { 90 if (context) return null; 91 if (event == null) { 93 return new Action [] { 94 SystemAction.get(RevertModificationsAction.class) 95 }; 96 } else { 97 return new Action [] { 98 new RollbackAction(), 99 SystemAction.get(RevertModificationsAction.class) 100 }; 101 } 102 } 103 104 private void initProperties() { 105 Sheet sheet = Sheet.createDefault(); 106 Sheet.Set ps = Sheet.createPropertiesSet(); 107 108 ps.put(new DateProperty()); 109 ps.put(new UsernameProperty()); 110 ps.put(new MessageProperty()); 111 112 sheet.put(ps); 113 setSheet(sheet); 114 } 115 116 private abstract class CommitNodeProperty<T> extends PropertySupport.ReadOnly<T> { 117 118 protected CommitNodeProperty(String name, Class <T> type, String displayName, String shortDescription) { 119 super(name, type, displayName, shortDescription); 120 } 121 122 public String toString() { 123 try { 124 return getValue().toString(); 125 } catch (Exception e) { 126 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 127 return e.getLocalizedMessage(); 128 } 129 } 130 131 public PropertyEditor getPropertyEditor() { 132 try { 133 return new RevisionPropertyEditor((String ) getValue()); 134 } catch (Exception e) { 135 return super.getPropertyEditor(); 136 } 137 } 138 } 139 140 private class UsernameProperty extends CommitNodeProperty { 141 142 public UsernameProperty() { 143 super(COLUMN_NAME_USERNAME, String .class, COLUMN_NAME_USERNAME, COLUMN_NAME_USERNAME); 144 } 145 146 public Object getValue() throws IllegalAccessException , InvocationTargetException { 147 if (event == null) { 148 return container.getLog().getAuthor(); 149 } else { 150 return ""; } 152 } 153 } 154 155 private class DateProperty extends CommitNodeProperty { 156 157 public DateProperty() { 158 super(COLUMN_NAME_DATE, String .class, COLUMN_NAME_DATE, COLUMN_NAME_DATE); 159 } 160 161 public Object getValue() throws IllegalAccessException , InvocationTargetException { 162 if (event == null) { 163 return DateFormat.getDateTimeInstance().format(container.getLog().getDate()); 164 } else { 165 return ""; } 167 } 168 } 169 170 private class MessageProperty extends CommitNodeProperty { 171 172 public MessageProperty() { 173 super(COLUMN_NAME_MESSAGE, String .class, COLUMN_NAME_MESSAGE, COLUMN_NAME_MESSAGE); 174 } 175 176 public Object getValue() throws IllegalAccessException , InvocationTargetException { 177 if (event == null) { 178 return container.getLog().getMessage(); 179 } else { 180 return ""; } 182 } 183 } 184 185 private class RollbackAction extends AbstractAction { 186 187 public RollbackAction() { 188 putValue(Action.NAME, NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackTo", event.getLogInfoHeader().getLog().getRevision().getNumber())); 190 } 191 192 public void actionPerformed(ActionEvent e) { 193 SummaryView.rollback(event); 194 } 195 } 196 197 private static class RevertModificationsAction extends NodeAction { 198 199 protected void performAction(Node[] activatedNodes) { 200 Set<RepositoryRevision.Event> events = new HashSet<RepositoryRevision.Event>(); 201 Set<RepositoryRevision> revisions = new HashSet<RepositoryRevision>(); 202 for (Node n : activatedNodes) { 203 RevisionNode node = (RevisionNode) n; 204 if (node.event != null) { 205 events.add(node.event); 206 } else { 207 revisions.add(node.container); 208 } 209 } 210 SearchHistoryPanel master = (SearchHistoryPanel) activatedNodes[0].getLookup().lookup(SearchHistoryPanel.class); 211 SummaryView.revert(master, revisions.toArray(new RepositoryRevision[revisions.size()]), events.toArray(new RepositoryRevision.Event[events.size()])); 212 } 213 214 protected boolean enable(Node[] activatedNodes) { 215 return true; 216 } 217 218 public String getName() { 219 return NbBundle.getMessage(RevisionNode.class, "CTL_Action_RollbackChange"); } 221 222 public HelpCtx getHelpCtx() { 223 return new HelpCtx(RevertModificationsAction.class); 224 } 225 } 226 227 private static class RevisionPropertyEditor extends PropertyEditorSupport { 228 229 private static final JLabel renderer = new JLabel(); 230 231 static { 232 renderer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); 233 } 234 235 public RevisionPropertyEditor(String value) { 236 setValue(value); 237 } 238 239 public void paintValue(Graphics gfx, Rectangle box) { 240 renderer.setForeground(gfx.getColor()); 241 renderer.setText((String ) getValue()); 242 renderer.setBounds(box); 243 renderer.paint(gfx); 244 } 245 246 public boolean isPaintable() { 247 return true; 248 } 249 } 250 } 251 | Popular Tags |