/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* 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.
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.constellation.swing;
import org.apache.sis.metadata.iso.DefaultMetadata;
import org.apache.sis.xml.MarshallerPool;
import org.constellation.admin.service.ConstellationClient;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import javax.swing.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
/**
*
* @author guilhem
*/
public class JProviderMetadataPane extends javax.swing.JPanel {
private final ConstellationClient serverV2;
private final String providerID;
private static final MarshallerPool POOL;
static {
MarshallerPool candidate = null;
try {
candidate = new MarshallerPool(JAXBContext.newInstance(DefaultMetadata.class), null);
} catch (JAXBException ex) {
Exceptions.printStackTrace(ex);
}
POOL = candidate;
}
/**
* Creates new form ProviderMetadataPane
*
* @param serverV2
* @param providerID
*/
public JProviderMetadataPane(final ConstellationClient serverV2, final String providerID) {
initComponents();
this.serverV2 = serverV2;
this.providerID = providerID;
try {
final Marshaller m = POOL.acquireMarshaller();
final DefaultMetadata meta = serverV2.providers.getMetadata(providerID);
if (meta != null) {
final StringWriter sw = new StringWriter();
m.marshal(meta, sw);
metadataArea.setText(sw.toString());
}
POOL.recycle(m);
} catch (IOException | JAXBException ex) {
Exceptions.printStackTrace(ex);
}
}
/**
* 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() {
saveMetadataButton = new JButton();
jScrollPane2 = new JScrollPane();
metadataArea = new JTextArea();
saveMetadataButton.setText(NbBundle.getMessage(JProviderMetadataPane.class, "save")); // NOI18N
saveMetadataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
saveMetadataButtonActionPerformed(evt);
}
});
metadataArea.setColumns(20);
metadataArea.setRows(5);
jScrollPane2.setViewportView(metadataArea);
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, GroupLayout.DEFAULT_SIZE, 917, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(saveMetadataButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 550, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(saveMetadataButton)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void saveMetadataButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_saveMetadataButtonActionPerformed
final String metadataXML = metadataArea.getText();
try {
final Unmarshaller um = POOL.acquireUnmarshaller();
final DefaultMetadata metadata = (DefaultMetadata) um.unmarshal(new StringReader(metadataXML));
POOL.recycle(um);
serverV2.providers.saveMetadata(providerID, metadata);
} catch (IOException | JAXBException ex) {
Exceptions.printStackTrace(ex);
}
}//GEN-LAST:event_saveMetadataButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private JScrollPane jScrollPane2;
private JTextArea metadataArea;
private JButton saveMetadataButton;
// End of variables declaration//GEN-END:variables
}