/** * Copyright 1999-2009 The Pegadi Team * * 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 org.pegadi.lister; // Java imports import org.pegadi.model.Article; import org.pegadi.model.ArticleStatus; import org.pegadi.model.LoginContext; import org.pegadi.model.PublishingMediaEnum; import org.pegadi.publisher.AbstractPublisher; import org.pegadi.publisher.FOPPDFPublisher; import org.pegadi.publisher.Publisher; import org.pegadi.publisher.StylesheetPublisher; import org.pegadi.publisher.io.CRLFFilterOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import javax.xml.transform.TransformerException; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.util.ResourceBundle; public class PublishDialog extends JDialog { Lister lister; Article article; GridBagLayout gridBag = new GridBagLayout(); JComboBox formatCombo; JComboBox platformCombo; JLabel formatLabel; JLabel platformLabel; String lastPublishedToDir; JButton saveButton; JButton cancelButton; String platform_current = "Current platform"; String platform_osx = "Mac OS X"; String platform_win = "Windows"; String platform_unix = "UTF-8"; ResourceBundle messages; private final Logger log = LoggerFactory.getLogger(getClass()); public PublishDialog(Lister lister, Article article) { super(lister); this.lister = lister; this.article = article; messages = ResourceBundle.getBundle("org.pegadi.lister.PublishDialogStrings"); createUI(); pack(); } public void createUI() { setTitle(messages.getString("dialog_title")); platform_current = messages.getString("platform_current"); formatLabel = new JLabel(messages.getString("format_label") + ":"); platformLabel = new JLabel(messages.getString("platform_label") + ":"); formatCombo = new JComboBox(); platformCombo = new JComboBox(); platformCombo.addItem(platform_win); platformCombo.addItem(platform_current); platformCombo.addItem(platform_unix); platformCombo.addItem(platform_osx); populateFormatCombo(); saveButton = new JButton(messages.getString("save_button")); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveButton_actionPerformed(); } }); cancelButton = new JButton(messages.getString("cancel_button")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); getContentPane().setLayout(gridBag); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 10; constraints.ipady = 10; gridBag.setConstraints(formatLabel, constraints); getContentPane().add(formatLabel); constraints = new GridBagConstraints(); constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 10; constraints.ipady = 10; gridBag.setConstraints(formatCombo, constraints); getContentPane().add(formatCombo); constraints = new GridBagConstraints(); constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 10; constraints.ipady = 10; gridBag.setConstraints(platformLabel, constraints); getContentPane().add(platformLabel); constraints = new GridBagConstraints(); constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 10; constraints.ipady = 10; gridBag.setConstraints(platformCombo, constraints); getContentPane().add(platformCombo); // A panel that holds the save and cancel buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(saveButton); buttonPanel.add(cancelButton); constraints = new GridBagConstraints(); constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; //constraints.ipadx=10; //constraints.ipady=10; gridBag.setConstraints(buttonPanel, constraints); getContentPane().add(buttonPanel); //setting location more properly Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); int x = Math.min(((size.width / 5) * 4), 810); int y = Math.min(((size.height / 5) * 4), 600); this.setLocation((size.width - x) / 2, (size.height - y) / 2); } public void setLastPublishedToDir(String lastPublishedToDir) { this.lastPublishedToDir = lastPublishedToDir; } public void populateFormatCombo() { formatCombo.setModel(new DefaultComboBoxModel(PublishingMediaEnum.values())); } public void saveButton_actionPerformed() { PublishingMediaEnum selected = (PublishingMediaEnum) formatCombo.getSelectedItem(); File saveFile = getSaveFile(article, selected.getFileSuffix()); if (saveFile == null) return; log.info("Publishing to file:" + saveFile.getAbsolutePath()); this.setCursor(new Cursor(Cursor.WAIT_CURSOR)); Publisher publisher; if (selected == PublishingMediaEnum.PDF) { publisher = new FOPPDFPublisher(); }else { publisher = new StylesheetPublisher(); Object selectedPlatform = platformCombo.getSelectedItem(); boolean isOnMac = selectedPlatform == platform_current && System.getProperty("os.name").startsWith("Mac OS X"); boolean isOnWindows = selectedPlatform == platform_current && System.getProperty("os.name").toLowerCase().indexOf("windows") > -1; if (selectedPlatform == platform_osx || isOnMac) { ((StylesheetPublisher)publisher).setArticleIndesignEncodingTag(article, "ANSI-MAC"); } else { ((StylesheetPublisher)publisher).setArticleIndesignEncodingTag(article, "ANSI-WIN"); } if(selectedPlatform == platform_osx){ publisher.setEncoding(AbstractPublisher.OutputEncoding.MacRoman); ((StylesheetPublisher)publisher).setMode(CRLFFilterOutputStream.Mode.MAC); }else if(selectedPlatform == platform_win){ publisher.setEncoding(AbstractPublisher.OutputEncoding.Cp1252); ((StylesheetPublisher)publisher).setMode(CRLFFilterOutputStream.Mode.WINDOWS); }else if(selectedPlatform == platform_unix){ publisher.setEncoding(AbstractPublisher.OutputEncoding.utf8); ((StylesheetPublisher)publisher).setMode(CRLFFilterOutputStream.Mode.UNIX); }else { publisher.setEncoding(AbstractPublisher.OutputEncoding.currentPlatform); if(isOnMac){ ((StylesheetPublisher)publisher).setMode(CRLFFilterOutputStream.Mode.MAC); }else if(isOnWindows){ ((StylesheetPublisher)publisher).setMode(CRLFFilterOutputStream.Mode.WINDOWS); } } } try { if(!article.parseText()){ throw new TransformerException("Could not serialize article"); } publisher.publish(article, saveFile, selected); JOptionPane.showMessageDialog(this, messages.getString("transform_ok"), messages.getString("transform_ok_captition"), JOptionPane.INFORMATION_MESSAGE); } catch (TransformerException e) { log.error("Error during transformation.", e); JOptionPane.showMessageDialog(this, messages.getString("transform_error_general"), messages.getString("transform_error_captition"), JOptionPane.ERROR_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(this, messages.getString("transform_error_save"), messages.getString("transform_error_save_captition"), JOptionPane.ERROR_MESSAGE); } catch (Exception e){ log.error("Error during transformation.", e); JOptionPane.showMessageDialog(this, messages.getString("transform_error_general"), messages.getString("transform_error_captition"), JOptionPane.ERROR_MESSAGE); } this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); int changeToStatus; // Try to parse the status_after_publish try { changeToStatus = Integer.parseInt(messages.getString("status_after_publish")); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog( this, messages.getString("NumberFormatException while parsing status_after_publish in ListerStrings.\n This should never happen!"), messages.getString("System error"), JOptionPane.INFORMATION_MESSAGE); return; } if (changeToStatus == article.getArticleStatus().getId()) { dispose(); return; } // Query the user and ask if the status should be updated. int changeStatus = JOptionPane.showConfirmDialog( this, messages.getString("publish_update_status"), messages.getString("publish_update_status_caption"), JOptionPane.YES_NO_OPTION); // The user pressed "Yes" if (changeStatus == JOptionPane.YES_OPTION) { // Get all statuses from the server java.util.List<ArticleStatus> allStatuses; try { allStatuses = LoginContext.server.getArticleStatuses(LoginContext.sessionKey); } catch (java.rmi.RemoteException rme) { log.error("Could not get article statuses from server. Server connection broken?", rme); JOptionPane.showMessageDialog(this, "Server error", "Error getting article statuses from server", JOptionPane.ERROR_MESSAGE); return; } // Search statuses for the changeToStatus ID ArticleStatus setStatus = null; for (ArticleStatus allStatuse : allStatuses) { if (allStatuse.getId() == changeToStatus) setStatus = allStatuse; } article.setArticleStatus(setStatus); Article hack = (Article) article.clone(); try { LoginContext.server.saveArticle(hack, LoginContext.sessionKey); } catch (java.rmi.RemoteException rme) { log.error("Could not save article with updated status to server", rme); JOptionPane.showMessageDialog(this, "Server error", "Error saving article status to server", JOptionPane.ERROR_MESSAGE); return; } lister.updateArticleList(); } dispose(); } public File getSaveFile(Article article, String extension) { //Create a file chooser final JFileChooser fc; if (lastPublishedToDir == null) fc = new JFileChooser(); else fc = new JFileChooser(lastPublishedToDir); fc.setSelectedFile(new File(article.suggestFileName(16) + extension)); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); //In response to a button click: int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); lastPublishedToDir = file.getParent(); lister.setLastPublishedToDir(lastPublishedToDir); if (file.exists()) { int theChoice = JOptionPane.showConfirmDialog(this, messages.getString("transform_error_file_exists"), messages.getString("transform_error_file_exists_captition"), JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION); if (theChoice == 2) { log.info("File exists. Publish aborted by user action."); return null; } } return file; } return null; } }