Java tutorial
/* * Copyright 2015 Torridity. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.tor.tribes.ui.components; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jdom2.Element; /** * * @author Torridity */ public class TimerPanel extends javax.swing.JPanel { private final Logger logger = LogManager.getLogger("TimerPanel"); private final SimpleDateFormat FORMAT = new SimpleDateFormat("HH:mm:ss:SSS"); private ActionListener listener = null; private String name = null; private long expires = 0; private String sound = null; public TimerPanel(ActionListener pListener) { this(pListener, null, 0, null); } /** * Creates new form TimerPanel */ public TimerPanel(ActionListener pListener, String pName, long pExpires, String pSound) { initComponents(); listener = pListener; name = pName; expires = pExpires; jLabel4.setText(name); sound = pSound; } /** * 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() { java.awt.GridBagConstraints gridBagConstraints; jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jSelectionBox = new javax.swing.JCheckBox(); setLayout(new java.awt.GridBagLayout()); jLabel4.setBackground(new java.awt.Color(204, 204, 204)); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel4.setText("Title"); jLabel4.setMaximumSize(new java.awt.Dimension(20, 18)); jLabel4.setMinimumSize(new java.awt.Dimension(20, 18)); jLabel4.setOpaque(true); jLabel4.setPreferredSize(new java.awt.Dimension(20, 18)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weightx = 1.0; add(jLabel4, gridBagConstraints); jLabel5.setText("00:00:00.000"); jLabel5.setMaximumSize(new java.awt.Dimension(66, 18)); jLabel5.setMinimumSize(new java.awt.Dimension(66, 18)); jLabel5.setPreferredSize(new java.awt.Dimension(66, 18)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weightx = 1.0; add(jLabel5, gridBagConstraints); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/ui/red_x.png"))); // NOI18N jLabel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(java.awt.event.MouseEvent evt) { fireRemoveTimerEvent(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; add(jLabel1, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; add(jSelectionBox, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents private void fireRemoveTimerEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireRemoveTimerEvent listener.actionPerformed(new ActionEvent(this, 0, "RemoveTimer")); }//GEN-LAST:event_fireRemoveTimerEvent public void update() { long remaining = expires - System.currentTimeMillis(); remaining = Math.max(remaining, 0); jLabel5.setText(DurationFormatUtils.formatDuration(remaining, "dd't' MM'm' yy'y' HH:mm:ss.SSS", true)); } public String getSound() { return sound; } public void setExpires(long pValue) { expires = pValue; } public void setSound(String pSound) { sound = pSound; } public void setTimerName(String pName) { name = pName; jLabel4.setText(name); } public boolean isSelected() { return jSelectionBox.isSelected(); } public boolean isExpired() { return expires - System.currentTimeMillis() <= 0; } public Element toXml(String elementName) { Element timer = new Element(elementName); timer.addContent(new Element("name").setText(name)); timer.addContent(new Element("expires").setText(Long.toString(expires))); timer.addContent(new Element("alert").setText(sound)); return timer; } public boolean fromXml(Element e) { try { setName(e.getChild("name").getValue()); expires = Long.parseLong(e.getChild("expires").getValue()); sound = e.getChild("alert").getValue(); } catch (Exception ex) { logger.debug("Couldn't load", e); return false; } return true; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JCheckBox jSelectionBox; // End of variables declaration//GEN-END:variables }