/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2015 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common * Development and Distribution License("CDDL") (collectively, the * "License"). You may not use this file except in compliance with the * License. You can obtain a copy of the License at * http://www.netbeans.org/cddl-gplv2.html * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the * specific language governing permissions and limitations under the * License. When distributing the software, include this License Header * Notice in each file and include the License file at * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the * License Header, with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * If you wish your version of this file to be governed by only the CDDL * or only the GPL Version 2, indicate your decision by adding * "[Contributor] elects to include this software in this distribution * under the [CDDL or GPL Version 2] license." If you do not indicate a * single choice of license, a recipient has the option to distribute * your version of this file under either the CDDL, the GPL Version 2 or * to extend the choice of license to its licensees as provided above. * However, if you add GPL Version 2 code and therefore, elected the GPL * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. * * Contributor(s): * * Portions Copyrighted 2015 Sun Microsystems, Inc. */ package com.junichi11.netbeans.modules.github.issues.issue.ui; import com.junichi11.netbeans.modules.github.issues.GitHubIssues; import java.awt.BorderLayout; import java.awt.Component; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JLabel; import javax.swing.SwingUtilities; import org.eclipse.egit.github.core.CommitFile; import org.eclipse.egit.github.core.PullRequestMarker; import org.eclipse.egit.github.core.Repository; import org.eclipse.egit.github.core.User; import org.netbeans.api.diff.Diff; import org.netbeans.api.diff.DiffView; import org.netbeans.api.diff.StreamSource; import org.netbeans.api.progress.ProgressHandle; import org.openide.util.Exceptions; import org.openide.util.NbBundle; import org.openide.util.RequestProcessor; /** * * @author junichi11 */ public class FileChangedPanel extends javax.swing.JPanel { private static final long serialVersionUID = 5842222768556582282L; private static final Logger LOGGER = Logger.getLogger(FileChangedPanel.class.getName()); private final CommitFile file; private final PullRequestMarker marker; private Component diffView; /** * Creates new form FileChendedPanel */ public FileChangedPanel(CommitFile file, PullRequestMarker marker) { this.file = file; this.marker = marker; initComponents(); String filename = file.getFilename(); fileNameLabel.setText(filename); diffPanel.setVisible(false); setAdditions(file.getAdditions()); setDeletions(file.getDeletions()); } private void setAdditions(int lines) { additionsLabel.setText("+" + String.valueOf(lines)); // NOI18N additionsLabel.setForeground(GitHubIssues.GREEN_COLOR); } private void setDeletions(int lines) { deletionsLabel.setText("-" + String.valueOf(lines)); // NOI18N deletionsLabel.setForeground(GitHubIssues.RED_COLOR); } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { fileNameLabel = new javax.swing.JLabel(); diffButton = new javax.swing.JButton(); diffPanel = new javax.swing.JPanel(); jSeparator1 = new javax.swing.JSeparator(); deletionsLabel = new javax.swing.JLabel(); additionsLabel = new javax.swing.JLabel(); org.openide.awt.Mnemonics.setLocalizedText(fileNameLabel, org.openide.util.NbBundle.getMessage(FileChangedPanel.class, "FileChangedPanel.fileNameLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(diffButton, org.openide.util.NbBundle.getMessage(FileChangedPanel.class, "FileChangedPanel.diffButton.text")); // NOI18N diffButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { diffButtonActionPerformed(evt); } }); diffPanel.setMaximumSize(new java.awt.Dimension(2147483647, 500)); diffPanel.setLayout(new java.awt.BorderLayout()); org.openide.awt.Mnemonics.setLocalizedText(deletionsLabel, org.openide.util.NbBundle.getMessage(FileChangedPanel.class, "FileChangedPanel.deletionsLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(additionsLabel, org.openide.util.NbBundle.getMessage(FileChangedPanel.class, "FileChangedPanel.additionsLabel.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(fileNameLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE) .addComponent(additionsLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(deletionsLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(diffButton)) .addComponent(diffPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator1) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(diffButton) .addComponent(fileNameLabel) .addComponent(deletionsLabel) .addComponent(additionsLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(diffPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0)) ); }// </editor-fold>//GEN-END:initComponents @NbBundle.Messages("FileChangedPanel.fetching.files=Fetching files...") private void diffButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_diffButtonActionPerformed if (file == null) { return; } // toggle boolean isVisible = diffPanel.isVisible(); diffPanel.setVisible(!isVisible); if (diffView != null) { return; } final String rawUrl = file.getRawUrl(); if (rawUrl != null) { RequestProcessor rp = GitHubIssues.getInstance().getRequestProcessor(); rp.post(new Runnable() { @Override public void run() { String filename = file.getFilename(); Repository repo = marker.getRepo(); User owner = repo.getOwner(); String name = repo.getName(); String baseSha = marker.getSha(); final JLabel fetchingLabel = new JLabel(Bundle.FileChangedPanel_fetching_files()); diffPanel.add(fetchingLabel, BorderLayout.NORTH); ProgressHandle handle = ProgressHandle.createHandle(Bundle.FileChangedPanel_fetching_files()); try { handle.start(); URL pullRequestRawUrl = new URL(rawUrl); URL baseRawUrl = new URL(String.format(GitHubIssues.RAW_URL_FORMAT, owner.getLogin(), name, baseSha, filename)); InputStream prInputStream; InputStream baseInputStream; try { prInputStream = pullRequestRawUrl.openStream(); } catch (FileNotFoundException ex) { prInputStream = new ByteArrayInputStream("".getBytes()); // NOI18N } try { baseInputStream = baseRawUrl.openStream(); } catch (FileNotFoundException ex) { baseInputStream = new ByteArrayInputStream("".getBytes()); // NOI18N } // don't close readers // if close readers, StreamSource cannot use them... final Reader prReader; final Reader baseReader; prReader = new BufferedReader(new InputStreamReader(prInputStream, StandardCharsets.UTF_8)); baseReader = new BufferedReader(new InputStreamReader(baseInputStream, StandardCharsets.UTF_8)); // XXX mime type String mimeType = getMimeType(filename); String prSha = file.getSha(); StreamSource prSource = StreamSource.createSource(filename, prSha, mimeType, prReader); // NOI18N StreamSource baseSource = StreamSource.createSource(filename, baseSha, mimeType, baseReader); // NOI18N final DiffView view = Diff.getDefault().createDiff(baseSource, prSource); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { diffView = view.getComponent(); diffPanel.add(diffView, BorderLayout.CENTER); } }); } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { LOGGER.log(Level.WARNING, null, ex); } finally { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { diffPanel.remove(fetchingLabel); } }); handle.finish(); } } }); } }//GEN-LAST:event_diffButtonActionPerformed private static String getMimeType(String fileName) { // XXX mime type return ""; // FileSystem fileSystem = FileUtil.createMemoryFileSystem(); // FileObject root = fileSystem.getRoot(); // int lastIndexOf = fileName.lastIndexOf("/"); // NOI18N // if (lastIndexOf == -1) { // return ""; // NOI18N // } // String name = fileName.substring(lastIndexOf + 1); // String ext = FileUtil.getExtension(fileName); // int lastIndexOfDot = name.lastIndexOf("."); // NOI18N // if (lastIndexOf != -1) { // name = name.substring(0, lastIndexOfDot); // } // try { // FileObject fileObject = root.createData(name, ext); // if (fileObject != null) { // return fileObject.getMIMEType(); // } // } catch (IOException ex) { // Exceptions.printStackTrace(ex); // } // return ""; // NOI18N } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel additionsLabel; private javax.swing.JLabel deletionsLabel; private javax.swing.JButton diffButton; private javax.swing.JPanel diffPanel; private javax.swing.JLabel fileNameLabel; private javax.swing.JSeparator jSeparator1; // End of variables declaration//GEN-END:variables }