1 19 20 package org.netbeans.modules.subversion.ui.status; 21 22 import javax.swing.SwingUtilities ; 23 import org.openide.util.*; 24 import org.openide.windows.TopComponent; 25 import org.openide.windows.WindowManager; 26 import org.openide.util.NbBundle; 27 import org.openide.util.HelpCtx; 28 import org.openide.ErrorManager; 29 import org.netbeans.modules.subversion.util.Context; 30 31 import java.awt.BorderLayout ; 32 import java.awt.Cursor ; 33 import java.io.*; 34 35 40 public class SvnVersioningTopComponent extends TopComponent implements Externalizable { 41 42 private static final long serialVersionUID = 1L; 43 44 private VersioningPanel syncPanel; 45 private Context context; 46 private String contentTitle; 47 private String branchTitle; 48 private long lastUpdateTimestamp; 49 50 private static SvnVersioningTopComponent instance; 51 52 public SvnVersioningTopComponent() { 53 setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title")); setIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/subversion/resources/icons/versioning-view.png")); setLayout(new BorderLayout ()); 56 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title")); 57 syncPanel = new VersioningPanel(this); 58 add(syncPanel); 59 } 60 61 public HelpCtx getHelpCtx() { 62 return new HelpCtx(getClass()); 63 } 64 65 protected void componentActivated() { 66 updateTitle(); 67 syncPanel.focus(); 68 } 69 70 public void writeExternal(ObjectOutput out) throws IOException { 71 super.writeExternal(out); 72 out.writeObject(context); 73 out.writeObject(contentTitle); 74 out.writeLong(lastUpdateTimestamp); 75 } 76 77 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 78 super.readExternal(in); 79 context = (Context) in.readObject(); 80 contentTitle = (String ) in.readObject(); 81 lastUpdateTimestamp = in.readLong(); 82 syncPanel.deserialize(); 83 } 84 85 protected void componentOpened() { 86 super.componentOpened(); 87 refreshContent(); 88 } 89 90 protected void componentClosed() { 91 super.componentClosed(); 92 } 93 94 private void refreshContent() { 95 if (syncPanel == null) return; updateTitle(); 97 syncPanel.setContext(context); 98 } 99 100 106 public void setContentTitle(String contentTitle) { 107 this.contentTitle = contentTitle; 108 updateTitle(); 109 } 110 111 117 void setBranchTitle(String branchTitle) { 118 this.branchTitle = branchTitle; 119 updateTitle(); 120 } 121 122 public void contentRefreshed() { 123 lastUpdateTimestamp = System.currentTimeMillis(); 124 updateTitle(); 125 } 126 127 private void updateTitle() { 128 final String age = computeAge(System.currentTimeMillis() - lastUpdateTimestamp); 129 SwingUtilities.invokeLater(new Runnable (){ 130 public void run() { 131 if (contentTitle == null) { 132 setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title")); } else { 134 if (branchTitle == null) { 135 setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_MultiTitle", contentTitle, age)); } else { 137 setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title_ContentBranch", contentTitle, branchTitle, age)); } 139 } 140 } 141 }); 142 } 143 144 String getContentTitle() { 145 return contentTitle; 146 } 147 148 private String computeAge(long l) { 149 if (lastUpdateTimestamp == 0) { 150 return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeUnknown"); } else if (l < 1000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeCurrent"); } else if (l < 2000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneSecond"); } else if (l < 60000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeSeconds", Long.toString(l / 1000)); } else if (l < 120000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneMinute"); } else if (l < 3600000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeMinutes", Long.toString(l / 60000)); } else if (l < 7200000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneHour"); } else if (l < 86400000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeHours", Long.toString(l / 3600000)); } else if (l < 172800000) { return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneDay"); } else { 168 return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeDays", Long.toString(l / 86400000)); } 170 } 171 172 public static synchronized SvnVersioningTopComponent getInstance() { 173 if (instance == null) { 174 instance = (SvnVersioningTopComponent) WindowManager.getDefault().findTopComponent("svnversioning"); if (instance == null) { 176 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ( 177 "Can not find Versioning component")); instance = new SvnVersioningTopComponent(); 179 } 180 } 181 182 return instance; 183 } 184 185 public Object readResolve() { 186 return getInstance(); 187 } 188 189 192 public RequestProcessor.Task performRefreshAction() { 193 return syncPanel.performRefreshAction(); 194 } 195 196 201 public void setContext(Context ctx) { 202 syncPanel.cancelRefresh(); 203 if (ctx == null) { 204 setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "MSG_Preparing")); setEnabled(false); 206 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 207 } else { 208 setEnabled(true); 209 setCursor(Cursor.getDefaultCursor()); 210 context = ctx; 211 setBranchTitle(null); 212 refreshContent(); 213 } 214 setToolTipText(getContextFilesList(ctx, NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title"))); } 216 217 private String getContextFilesList(Context ctx, String def) { 218 if (ctx == null || ctx.getFiles().length == 0) return def; 219 StringBuffer sb = new StringBuffer (200); 220 sb.append("<html>"); for (File file : ctx.getFiles()) { 222 sb.append(file.getAbsolutePath()); 223 sb.append("<br>"); } 225 sb.delete(sb.length() - 4, Integer.MAX_VALUE); 226 return sb.toString(); 227 } 228 229 230 public boolean hasContext() { 231 return context != null && context.getFiles().length > 0; 232 } 233 234 protected String preferredID() { 235 return "synchronize"; } 237 238 public int getPersistenceType() { 239 return TopComponent.PERSISTENCE_ALWAYS; 240 } 241 } 242 | Popular Tags |