1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.syncview; 21 22 import org.openide.windows.TopComponent; 23 import org.openide.windows.WindowManager; 24 import org.openide.util.NbBundle; 25 import org.openide.util.HelpCtx; 26 import org.openide.ErrorManager; 27 import org.netbeans.modules.versioning.system.cvss.util.Context; 28 29 import java.awt.*; 30 import java.io.*; 31 32 37 public class CvsSynchronizeTopComponent extends TopComponent implements Externalizable { 38 39 private static final long serialVersionUID = 1L; 40 41 private SynchronizePanel syncPanel; 42 private Context context; 43 private String contentTitle; 44 private String branchTitle; 45 private long lastUpdateTimestamp; 46 47 private static CvsSynchronizeTopComponent instance; 48 49 public CvsSynchronizeTopComponent() { 50 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title")); 51 setIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/versioning-view.png", true)); setLayout(new BorderLayout()); 53 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "ACSD_Synchronize_TopComponent")); 54 syncPanel = new SynchronizePanel(this); 55 add(syncPanel); 56 setFocusable(true); 57 } 58 59 public HelpCtx getHelpCtx() { 60 return new HelpCtx(getClass()); 61 } 62 63 protected void componentActivated() { 64 updateTitle(); 65 syncPanel.focus(); 66 } 67 68 public void writeExternal(ObjectOutput out) throws IOException { 69 super.writeExternal(out); 70 out.writeObject(context); 71 out.writeObject(contentTitle); 72 out.writeLong(lastUpdateTimestamp); 73 } 74 75 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 76 super.readExternal(in); 77 context = (Context) in.readObject(); 78 contentTitle = (String ) in.readObject(); 79 lastUpdateTimestamp = in.readLong(); 80 syncPanel.deserialize(); 81 } 82 83 protected void componentOpened() { 84 super.componentOpened(); 85 refreshContent(); 86 } 87 88 protected void componentClosed() { 89 super.componentClosed(); 90 } 91 92 private void refreshContent() { 93 if (syncPanel == null) return; updateTitle(); 95 syncPanel.setContext(context); 96 } 97 98 104 public void setContentTitle(String contentTitle) { 105 this.contentTitle = contentTitle; 106 updateTitle(); 107 } 108 109 115 void setBranchTitle(String branchTitle) { 116 this.branchTitle = branchTitle; 117 updateTitle(); 118 } 119 120 public void contentRefreshed() { 121 lastUpdateTimestamp = System.currentTimeMillis(); 122 updateTitle(); 123 } 124 125 private void updateTitle() { 126 String age = computeAge(System.currentTimeMillis() - lastUpdateTimestamp); 127 if (contentTitle == null) { 128 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title")); } else { 130 if (branchTitle == null) { 131 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_MultiTitle", contentTitle, age)); 132 } else { 133 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title_ContentBranch", contentTitle, branchTitle, age)); 134 } 135 } 136 } 137 138 String getContentTitle() { 139 return contentTitle; 140 } 141 142 private String computeAge(long l) { 143 if (lastUpdateTimestamp == 0) { 144 return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeUnknown"); 145 } else if (l < 1000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeCurrent"); 147 } else if (l < 2000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneSecond"); 149 } else if (l < 60000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeSeconds", Long.toString(l / 1000)); 151 } else if (l < 120000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneMinute"); 153 } else if (l < 3600000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeMinutes", Long.toString(l / 60000)); 155 } else if (l < 7200000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneHour"); 157 } else if (l < 86400000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeHours", Long.toString(l / 3600000)); 159 } else if (l < 172800000) { return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneDay"); 161 } else { 162 return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeDays", Long.toString(l / 86400000)); 163 } 164 } 165 166 public static synchronized CvsSynchronizeTopComponent getInstance() { 167 if (instance == null) { 168 instance = (CvsSynchronizeTopComponent) WindowManager.getDefault().findTopComponent("synchronize"); if (instance == null) { 170 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ( 171 "Can not find Versioning component")); instance = new CvsSynchronizeTopComponent(); 173 } 174 } 175 176 return instance; 177 } 178 179 public Object readResolve() { 180 return getInstance(); 181 } 182 183 186 public void performRefreshAction() { 187 syncPanel.performRefreshAction(); 188 } 189 190 public static final class ReadResolver implements java.io.Serializable { 191 public Object readResolve() { 192 if(instance == null) { 193 instance = new CvsSynchronizeTopComponent(); 194 } 195 return instance; 196 } 197 } 198 199 208 public void setContext(Context ctx) { 209 syncPanel.cancelRefresh(); 210 if (ctx == null) { 211 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "BK1001")); 212 setEnabled(false); 213 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 214 } else { 215 setEnabled(true); 216 setCursor(Cursor.getDefaultCursor()); 217 context = ctx; 218 setBranchTitle(null); 219 refreshContent(); 220 } 221 setToolTipText(getContextFilesList(ctx, NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title"))); 222 } 223 224 private String getContextFilesList(Context ctx, String def) { 225 if (ctx == null || ctx.getFiles().length == 0) return def; 226 StringBuilder sb = new StringBuilder (200); 227 sb.append("<html>"); for (File file : ctx.getFiles()) { 229 sb.append(file.getAbsolutePath()); 230 sb.append("<br>"); } 232 sb.delete(sb.length() - 4, Integer.MAX_VALUE); 233 return sb.toString(); 234 } 235 236 237 public boolean hasContext() { 238 return context != null && context.getFiles().length > 0; 239 } 240 241 protected String preferredID() { 242 return "synchronize"; } 244 245 public int getPersistenceType() { 246 return TopComponent.PERSISTENCE_ALWAYS; 247 } 248 } 249 | Popular Tags |