Java tutorial
/* * Copyright (c) 2016 Sebastian Brudzinski * * See the file LICENSE for copying permission. */ package latexstudio.editor; import com.dropbox.core.DbxClient; import com.dropbox.core.DbxEntry; import com.dropbox.core.DbxException; import java.io.File; import java.io.IOException; import latexstudio.editor.remote.DbxEntryDto; import latexstudio.editor.remote.DbxState; import latexstudio.editor.remote.DbxUtil; import latexstudio.editor.util.ApplicationUtils; import org.apache.commons.io.FileUtils; import org.netbeans.api.settings.ConvertAsProperties; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.util.Exceptions; import org.openide.util.NbBundle.Messages; import org.openide.windows.TopComponent; /** * Top component which displays the past revision of a Dropbox file. The code * within this component is not editable and grayed out. */ @ConvertAsProperties(dtd = "-//latexstudio.editor//RevisionDisplay//EN", autostore = false) @TopComponent.Description(preferredID = "RevisionDisplayTopComponent", //iconBase="SET/PATH/TO/ICON/HERE", persistenceType = TopComponent.PERSISTENCE_ALWAYS) @TopComponent.Registration(mode = "editor", openAtStartup = false) @ActionID(category = "Window", id = "latexstudio.editor.RevisionDisplayTopComponent") @ActionReference(path = "Menu/Window" /*, position = 333 */) @TopComponent.OpenActionRegistration(displayName = "#CTL_RevisionDisplayAction", preferredID = "RevisionDisplayTopComponent") @Messages({ "CTL_RevisionDisplayAction=Revision Display", "CTL_RevisionDisplayTopComponent=Dropbox revision display", "HINT_RevisionDisplayTopComponent=This is a Revision display window" }) public final class RevisionDisplayTopComponent extends TopComponent { private final EditorTopComponent etc = new TopComponentFactory<EditorTopComponent>() .getTopComponent(EditorTopComponent.class.getSimpleName()); private DbxState displayedRevision; public RevisionDisplayTopComponent() { initComponents(); setName(Bundle.CTL_RevisionDisplayTopComponent()); setToolTipText(Bundle.HINT_RevisionDisplayTopComponent()); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); rSyntaxTextArea = new org.fife.ui.rsyntaxtextarea.RSyntaxTextArea(); jButton1 = new javax.swing.JButton(); rSyntaxTextArea.setEditable(false); rSyntaxTextArea.setBackground(new java.awt.Color(204, 204, 204)); rSyntaxTextArea.setColumns(20); rSyntaxTextArea.setRows(5); rSyntaxTextArea .setSyntaxEditingStyle(org.openide.util.NbBundle.getMessage(RevisionDisplayTopComponent.class, "RevisionDisplayTopComponent.rSyntaxTextArea.syntaxEditingStyle")); // NOI18N jScrollPane1.setViewportView(rSyntaxTextArea); org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle .getMessage(RevisionDisplayTopComponent.class, "RevisionDisplayTopComponent.jButton1.text")); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed DbxClient client = DbxUtil.getDbxClient(); if (client == null) { return; } if (displayedRevision != null) { try { DbxEntry.File restoredFile = client.restoreFile(displayedRevision.getPath(), displayedRevision.getRevision()); File restored = DbxUtil.downloadRemoteFile(new DbxEntryDto(restoredFile), ApplicationUtils.getTempSourceFile()); etc.setEditorContent(FileUtils.readFileToString(restored)); etc.requestActive(); } catch (DbxException e) { DbxUtil.showDbxAccessDeniedPrompt(); } catch (IOException e) { Exceptions.printStackTrace(e); } } }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private org.fife.ui.rsyntaxtextarea.RSyntaxTextArea rSyntaxTextArea; // End of variables declaration//GEN-END:variables @Override public void componentOpened() { // TODO add custom code on component opening } @Override public void componentClosed() { displayedRevision = null; setText(null); setDisplayName("Dropbox revision display"); } void writeProperties(java.util.Properties p) { // better to version settings since initial version as advocated at // http://wiki.apidesign.org/wiki/PropertyFiles p.setProperty("version", "1.0"); // TODO store your settings } void readProperties(java.util.Properties p) { String version = p.getProperty("version"); // TODO read your settings according to their version } public void setText(String text) { rSyntaxTextArea.setText(text); } public DbxState getDisplayedRevision() { return displayedRevision; } public void setDisplayedRevision(DbxState displayedRevision) { this.displayedRevision = displayedRevision; } }