com.junichi11.netbeans.modules.backlog.issue.ui.AttachmentPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.junichi11.netbeans.modules.backlog.issue.ui.AttachmentPanel.java

Source

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 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 2014 Sun Microsystems, Inc.
 */
package com.junichi11.netbeans.modules.backlog.issue.ui;

import com.nulabinc.backlog4j.Attachment;
import com.nulabinc.backlog4j.User;
import javax.swing.Action;
import org.apache.commons.io.FileUtils;
import org.netbeans.api.annotations.common.CheckForNull;
import com.junichi11.netbeans.modules.backlog.issue.BacklogAttachment;
import com.junichi11.netbeans.modules.backlog.utils.UiUtils;
import org.openide.util.NbBundle;

/**
 *
 * @author junichi11
 */
public class AttachmentPanel extends javax.swing.JPanel {

    static final String PROP_ATTACHMENT_DELETED = "attachment.deleted"; // NOI18N
    private static final long serialVersionUID = 7053870845711777497L;
    private BacklogAttachment backlogAttachment;
    private Attachment attachment;

    /**
     * Creates new form UnsubmittedAttachmentPanel
     */
    public AttachmentPanel(BacklogAttachment backlogAttachment) {
        this(backlogAttachment.getAttachment());
        this.backlogAttachment = backlogAttachment;
    }

    public AttachmentPanel(Attachment attachment) {
        this.attachment = attachment;
        initComponents();
        namemLabel.setText(attachment.getName());
        sizeLabel.setText(String.valueOf(FileUtils.byteCountToDisplaySize(attachment.getSize())));
        User createdUser = attachment.getCreatedUser();
        if (createdUser != null) {
            setToolTipText(String.format("Created by %s", createdUser.getName()));
        } else {
            openLinkButton.setVisible(false);
            downloadLinkButton.setVisible(false);
        }
        statusLabel.setText(""); // NOI18N
    }

    public Attachment getAttachment() {
        return attachment;
    }

    @CheckForNull
    public BacklogAttachment getBacklogAttachment() {
        return backlogAttachment;
    }

    public boolean isDeleted() {
        return !isVisible();
    }

    public boolean isUnsubmitted() {
        return attachment.getCreatedUser() == null;
    }

    public void setStatus(String message) {
        if (message == null) {
            return;
        }
        statusLabel.setText(message);
    }

    /**
     * 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() {

        namemLabel = new javax.swing.JLabel();
        deleteLinkButton = new org.netbeans.modules.bugtracking.commons.LinkButton();
        sizeLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        downloadLinkButton = new org.netbeans.modules.bugtracking.commons.LinkButton();
        openLinkButton = new org.netbeans.modules.bugtracking.commons.LinkButton();
        statusLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(namemLabel,
                org.openide.util.NbBundle.getMessage(AttachmentPanel.class, "AttachmentPanel.namemLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(deleteLinkButton, org.openide.util.NbBundle
                .getMessage(AttachmentPanel.class, "AttachmentPanel.deleteLinkButton.text")); // NOI18N
        deleteLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteLinkButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(sizeLabel,
                org.openide.util.NbBundle.getMessage(AttachmentPanel.class, "AttachmentPanel.sizeLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(downloadLinkButton, org.openide.util.NbBundle
                .getMessage(AttachmentPanel.class, "AttachmentPanel.downloadLinkButton.text")); // NOI18N
        downloadLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downloadLinkButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(openLinkButton,
                org.openide.util.NbBundle.getMessage(AttachmentPanel.class, "AttachmentPanel.openLinkButton.text")); // NOI18N
        openLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openLinkButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(statusLabel,
                org.openide.util.NbBundle.getMessage(AttachmentPanel.class, "AttachmentPanel.statusLabel.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().addContainerGap().addComponent(namemLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(sizeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                        .addComponent(statusLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(downloadLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(namemLabel)
                        .addComponent(deleteLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(sizeLabel)
                        .addComponent(downloadLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(openLinkButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(statusLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));
    }// </editor-fold>//GEN-END:initComponents

    @NbBundle.Messages({ "# {0} - name",
            "AttachmentPanel.message.delete.attachment=Do you really want to delete {0}?" })
    private void deleteLinkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteLinkButtonActionPerformed
        if (!isUnsubmitted()) {
            if (!UiUtils
                    .showQuestionDialog(Bundle.AttachmentPanel_message_delete_attachment(attachment.getName()))) {
                return;
            }
        }
        setVisible(false);
        firePropertyChange(PROP_ATTACHMENT_DELETED, null, null);
    }//GEN-LAST:event_deleteLinkButtonActionPerformed

    private void openLinkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openLinkButtonActionPerformed
        if (backlogAttachment != null) {
            Action openAction = backlogAttachment.getOpenAction();
            openAction.actionPerformed(evt);
        }
    }//GEN-LAST:event_openLinkButtonActionPerformed

    private void downloadLinkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadLinkButtonActionPerformed
        if (backlogAttachment != null) {
            Action saveAction = backlogAttachment.getSaveAction();
            saveAction.actionPerformed(evt);
        }
    }//GEN-LAST:event_downloadLinkButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.netbeans.modules.bugtracking.commons.LinkButton deleteLinkButton;
    private org.netbeans.modules.bugtracking.commons.LinkButton downloadLinkButton;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel namemLabel;
    private org.netbeans.modules.bugtracking.commons.LinkButton openLinkButton;
    private javax.swing.JLabel sizeLabel;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
}