/*
* Copyright (C) 2014 GG-Net GmbH - Oliver Günther
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.ggnet.dwoss.redtape;
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import org.openide.util.lookup.ServiceProvider;
import eu.ggnet.dwoss.redtape.entity.Position;
import eu.ggnet.dwoss.stock.entity.Stock;
import eu.ggnet.dwoss.uniqueunit.api.PicoUnit;
import eu.ggnet.dwoss.uniqueunit.api.UnitShard;
import eu.ggnet.dwoss.util.*;
import eu.ggnet.dwoss.util.interactiveresult.Result;
import eu.ggnet.saft.core.*;
import eu.ggnet.saft.core.all.SelectionEnhancer;
import eu.ggnet.saft.core.authorisation.Guardian;
import eu.ggnet.saft.core.ops.Ops;
import eu.ggnet.saft.core.ops.Selector;
import static eu.ggnet.saft.core.Client.lookup;
/**
* View that is used to quickly check availability.
* <p/>
* @author pascal.perau, oliver.guenther
*/
@ServiceProvider(service = MainComponent.class)
public class UnitAvailabilityViewCask extends javax.swing.JPanel implements MainComponent {
public static class UnitShardRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if ( !(value instanceof UnitShard) ) return label;
UnitShard us = (UnitShard)value;
label.setText(us.getHtmlDescription());
label.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
label.setForeground(Color.black);
label.setBackground(getColor(us));
if ( cellHasFocus ) {
label.setBackground(SystemColor.textHighlight);
label.setForeground(SystemColor.textHighlightText);
}
return label;
}
private Color getColor(UnitShard us) {
if ( us.getAvailable() == null ) return Color.yellow;
if ( us.getAvailable() == false ) return Color.red;
// now we are available
if ( Objects.equals(us.getStockId(), lookup(Workspace.class).getValue(Stock.class).getId()) ) return Color.green;
return Color.cyan;
}
}
private final DefaultListModel<UnitShard> model = new DefaultListModel<>();
private final Selector<UnitShard> selector; // No clear needed. This Panel is in the MainFrame.
/** Creates new form UnitAvailability */
public UnitAvailabilityViewCask() {
initComponents();
resultList.setModel(model);
resultList.setCellRenderer(new UnitShardRenderer());
SelectionEnhancer<UnitShard> selectionEnhancer = (eu.ggnet.dwoss.uniqueunit.api.UnitShard selected) -> {
if ( selected != null && selected.getAvailable() != null )
return Arrays.asList(new PicoUnit(selected.getUniqueUnitId(), "SopoNr:" + selected.getRefurbishedId()));
return Collections.EMPTY_LIST;
};
selector = Ops.seletor(UnitShard.class, selectionEnhancer);
}
@Override
public String getLayoutHint() {
return BorderLayout.WEST;
}
/** 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;
jLabel1 = new javax.swing.JLabel();
searchCommand = new javax.swing.JTextField();
searchButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
resultList = new javax.swing.JList<UnitShard>();
setLayout(new java.awt.GridBagLayout());
jLabel1.setText("SopoNr.:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
add(jLabel1, gridBagConstraints);
searchCommand.setPreferredSize(new java.awt.Dimension(100, 23));
searchCommand.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 0.1;
add(searchCommand, gridBagConstraints);
searchButton.setText("Liste leeren");
searchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
add(searchButton, gridBagConstraints);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
resultList.setMaximumSize(new java.awt.Dimension(1000, 1000));
resultList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
resultListMouseClicked(evt);
}
});
resultList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
resultListValueChanged(evt);
}
});
jScrollPane1.setViewportView(resultList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 0.2;
gridBagConstraints.weighty = 0.2;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
add(jScrollPane1, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void resultListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultListMouseClicked
if ( evt.getClickCount() != 2 ) return;
UnitShard us = resultList.getSelectedValue();
if ( us == null || us.getAvailable() == null ) return;
Ui.call(() -> Client.lookup(UnitOverseer.class).toDetailedHtml(us.getRefurbishedId(), Client.lookup(Guardian.class).getUsername()))
.openFx(HtmlPane.class, us.getRefurbishedId())
.exec();
}//GEN-LAST:event_resultListMouseClicked
private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
String refurbishedId = searchCommand.getText().trim();
UnitShard us = lookup(UnitOverseer.class).find(refurbishedId);
model.add(0, us);
searchCommand.setText("");
}//GEN-LAST:event_searchActionPerformed
private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
model.clear();
}//GEN-LAST:event_clearActionPerformed
private void resultListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_resultListValueChanged
selector.selected(resultList.getSelectedValue());
}//GEN-LAST:event_resultListValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList<UnitShard> resultList;
private javax.swing.JButton searchButton;
private javax.swing.JTextField searchCommand;
// End of variables declaration//GEN-END:variables
public static void main(String[] args) {
Client.addSampleStub(UnitOverseer.class, new UnitOverseer() {
private Map<String, Tuple2<UnitShard, String>> data = new HashMap<>();
{
data.put("1", new Tuple2<>(new UnitShard("1", 1, "SopoNr.: 1", Boolean.TRUE, 1), "Details zu Unit 1"));
data.put("2", new Tuple2<>(new UnitShard("2", 2, "SopoNr.: 2", Boolean.FALSE, 1), "Details zu Unit 2"));
data.put("3", new Tuple2<>(new UnitShard("3", 3, "SopoNr.: 3", Boolean.TRUE, 1), "Details zu Unit 3"));
data.put("4", new Tuple2<>(new UnitShard("4", 4, "SopoNr.: 4", Boolean.FALSE, 1), "Details zu Unit 4"));
data.put("5", new Tuple2<>(new UnitShard("5", 5, "SopoNr.: 5 exitiert nicht", null, null), "Existiert Nicht"));
data.put("6", new Tuple2<>(new UnitShard("6", 6, "SopoNr.: 6", Boolean.FALSE, null), "Details zu Unit 6"));
}
@Override
public String toDetailedHtml(String refurbishId, String username) {
if ( !data.containsKey(refurbishId) ) return refurbishId + " existiert nicht";
return data.get(refurbishId)._2;
}
@Override
public UnitShard find(String refurbishId) {
if ( !data.containsKey(refurbishId) ) return new UnitShard(refurbishId, 0, "SopoNr.: " + refurbishId + " exitiert nicht", null, null);
return data.get(refurbishId)._1;
}
@Override
public boolean isAvailable(String refurbishId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void lockStockUnit(long dossierId, String refurbishedId) throws IllegalStateException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Result<List<Position>> createUnitPosition(String refurbishId, long documentId) throws UserInfoException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String toDetailedHtml(int uniqueUnitId, String username) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
OkCancelDialog<UnitAvailabilityViewCask> view = new OkCancelDialog<>("blub", new UnitAvailabilityViewCask());
view.setVisible(true);
view.dispose();
}
}