civideja
Član broj: 98858 Poruke: 25 *.dynamic.isp.telekom.rs.
|
Hvala vam puno! Ideja mi je jasna ali mi je realizacija ravna nuli - za 5h cackanja nisam uspeo da 'optimizujem' kod. Evo kompletnih klasa (ostavio sam pod komentarom ono sto sam cackao, pa mi ako mozete ukazite na greske): MainForm.java:
import java.awt.Color;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class MainForm extends javax.swing.JFrame {
private Integer red;
private Integer blue;
private Integer green;
private String s;
/** Creates new form MainForm */
public MainForm() {
initComponents();
}
String getData()
{
FileReader fr = null;
StringBuilder html = new StringBuilder();
try
{
fr = new FileReader ("template.html");
int b;
while ((b = fr.read())!= -1 )
{
html.append(Character.toString((char)b));
}
fr.close();
}
catch(IOException ex) {JOptionPane.showMessageDialog(epContent,"Error loading template!");
}
return html.toString();
}
// String insertData(String template)
// {
//insert menu items
/*
String[] menuOld = {"###menu1Link###", "###menu1Title###",
"###menu2Link###", "###menu2Title###", "###menu3Link###",
"###menu3Title###", "###menu4Link###", "###menu4Title###",
"###menu5Link###", "###menu5Title###", "###menu6Link###",
"###menu6Title###", "###content###", "###footer###",
"###image1###", "###image2###", "#FF9900"};
String[] menuNew = {tbMenu1Link.getText(), tbMenu1Title.getText(),
tbMenu2Title.getText(), tbMenu3Link.getText(), tbMenu3Title.getText(),
tbMenu4Link.getText(), tbMenu4Title.getText(), tbMenu5Link.getText(),
tbMenu5Title.getText(), tbMenu6Link.getText(), tbMenu6Title.getText(),
epContent.getText(), epFooter.getText(), tbImage1.getText(),
tbImage2.getText(), tbRgbColor.getText()};
// template = insertData1(template, menuOld, menuNew);
for (int i=0; i<menuOld.length; i++){
template = template.replace(menuOld, menuNew);}
// template = template.replaceAll(menuOld, menuNew);}
// return template;
return template;}
*/
/* String insertData1(String template, String[] menuOld, String[] menuNew) {
for (int i=0; i<menuOld.length; i++)
template = template.replace(menuOld, menuNew);
return template;
}
*/ // throw new UnsupportedOperationException("Not yet implemented");}
// String insertData(String template, String[] menuOld, String[] menuNew)
// {
// for (int i=0; i<menuOld.length; i++)
// template = template.replace(menuOld, menuNew);}
// return template; }
String insertData(String template) // Ovo treba da se optimizuje
{
template = template.replace("###menu1Link###", tbMenu1Link.getText());
template = template.replace("###menu1Title###", tbMenu1Title.getText());
template = template.replace("###menu2Link###", tbMenu2Link.getText());
template = template.replace("###menu2Title###", tbMenu2Title.getText());
template = template.replace("###menu3Link###", tbMenu3Link.getText());
template = template.replace("###menu3Title###", tbMenu3Title.getText());
template = template.replace("###menu4Link###", tbMenu4Link.getText());
template = template.replace("###menu4Title###", tbMenu4Title.getText());
template = template.replace("###menu5Link###", tbMenu5Link.getText());
template = template.replace("###menu5Title###", tbMenu5Title.getText());
template = template.replace("###menu6Link###", tbMenu6Link.getText());
template = template.replace("###menu6Title###", tbMenu6Title.getText());
//insert content
template = template.replace("###content###", epContent.getText());
//insert footer
template = template.replace("###footer###", epFooter.getText());
//insert images
template = template.replace("###image1###", tbImage1.getText());
template = template.replace("###image2###", tbImage2.getText());
//Insert rgb color in HTML
template = template.replace("#FF9900", tbRgbColor.getText()) ;
return template; }
void saveData(String data)
{
JFileChooser jfc = new JFileChooser();
jfc.setVisible(true);
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("html"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "html files";
}
});
int res = jfc.showSaveDialog(this);
if(res!=JFileChooser.CANCEL_OPTION&&jfc.getSelectedFile()!=null)
{
FileWriter fw = null;
try {
fw = new FileWriter(jfc.getSelectedFile());
fw.write(data);
fw.close();
JOptionPane.showMessageDialog(epContent,"Dokument je uspesno sacuvan!");
} catch (IOException ex) { }
finally {
try { fw.close(); } catch (IOException ex) { }
}
}
else JOptionPane.showMessageDialog(rootPane, "File not selected!");
}
/** 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">
private void initComponents() {
jLabelMenuOptions = new javax.swing.JLabel();
jLabelContentText = new javax.swing.JLabel();
jLabelFooterText = new javax.swing.JLabel();
jLabelImages = new javax.swing.JLabel();
tbMenu1Title = new javax.swing.JTextField();
tbMenu2Title = new javax.swing.JTextField();
tbMenu3Title = new javax.swing.JTextField();
tbMenu4Title = new javax.swing.JTextField();
tbMenu5Title = new javax.swing.JTextField();
tbMenu6Title = new javax.swing.JTextField();
tbMenu1Link = new javax.swing.JTextField();
tbMenu2Link = new javax.swing.JTextField();
tbMenu3Link = new javax.swing.JTextField();
tbMenu4Link = new javax.swing.JTextField();
tbMenu5Link = new javax.swing.JTextField();
tbMenu6Link = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
epContent = new javax.swing.JEditorPane();
jScrollPane2 = new javax.swing.JScrollPane();
epFooter = new javax.swing.JEditorPane();
tbImage2 = new javax.swing.JTextField();
btnGenerate = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
tbImage1 = new javax.swing.JTextField();
btnColorChange = new javax.swing.JButton();
tbRgbColor = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabelMenuOptions.setText("Menu options");
jLabelContentText.setText("Content text");
jLabelFooterText.setText("Footer text");
jLabelImages.setText("Images");
jScrollPane1.setViewportView(epContent);
jScrollPane2.setViewportView(epFooter);
tbImage2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbImage2MouseClicked(evt);
}
});
btnGenerate.setText("Generate");
btnGenerate.setToolTipText("Create your HTM code");
btnGenerate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGenerateActionPerformed(evt);
}
});
jButton1.setText("Add more");
jButton1.setToolTipText("Add more sites with description");
tbImage1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbImage1MouseClicked(evt);
}
});
btnColorChange.setText("Change colors");
btnColorChange.setToolTipText("Change colors of futer and menu in your HTML");
btnColorChange.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnColorChangeActionPerformed(evt);
}
});
tbRgbColor.setText("RGB Value");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(125, 125, 125)
.addComponent(jLabelMenuOptions))
.addGroup(layout.createSequentialGroup()
.addGap(125, 125, 125)
.addComponent(jLabelContentText))
.addGroup(layout.createSequentialGroup()
.addGap(126, 126, 126)
.addComponent(jLabelFooterText))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(btnGenerate)
.addGap(18, 18, 18)
.addComponent(btnColorChange)
.addGap(21, 21, 21)
.addComponent(tbRgbColor))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(tbImage1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tbImage2))
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tbMenu3Title)
.addComponent(tbMenu2Title)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tbMenu1Title, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addComponent(tbMenu4Title)
.addComponent(tbMenu6Title)
.addComponent(tbMenu5Title)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tbMenu3Link)
.addComponent(tbMenu2Link)
.addComponent(tbMenu1Link, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addComponent(tbMenu4Link)
.addComponent(tbMenu5Link)
.addComponent(tbMenu6Link))))
.addGap(18, 18, 18)
.addComponent(jButton1))))
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(jLabelImages)))
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabelMenuOptions)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu1Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu1Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tbMenu2Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu2Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbMenu3Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu3Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu4Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu4Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu5Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu5Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu6Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu6Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLabelContentText)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabelFooterText)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabelImages)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbImage2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbImage1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnGenerate)
.addComponent(tbRgbColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnColorChange))
.addGap(34, 34, 34))
);
pack();
}// </editor-fold>
private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {
saveData(insertData(getData()));
}
private void tbImage1MouseClicked(java.awt.event.MouseEvent evt) {
JFileChooser jfc = new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("jpg")|| e.equals("gif")|| e.equals("png")
|| e.equals("tif")|| e.equals("jpeg")|| e.equals("tiff"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "Image files";
}
});
int status = jfc.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION){
s =(jfc.getSelectedFile()).toString();
tbImage1.setText(s); }
else if (status ==JFileChooser.CANCEL_OPTION)
tbImage1.setText("Please select image file!");
}
private void btnColorChangeActionPerformed(java.awt.event.ActionEvent evt) {
JColorChooser jcc = new JColorChooser();
Color c = jcc.showDialog(null,"Chose color of menu and futer",Color.ORANGE);
red = c.getRed();
//tbMenu4Link.setText(red.toString());
green = c.getGreen();
//tbMenu5Link.setText(green.toString());
blue = c.getBlue();
//tbMenu6Link.setText(blue.toString());
tbRgbColor.setText("rgb(" + red.toString()+","+ green.toString()+","+blue.toString()+")");
}
private void tbImage2MouseClicked(java.awt.event.MouseEvent evt) {
JFileChooser jfc = new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("jpg")|| e.equals("gif")|| e.equals("png")
|| e.equals("tif")|| e.equals("jpeg")|| e.equals("tiff"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "Image files";
}
});
int status = jfc.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION){
s =(jfc.getSelectedFile()).toString();
tbImage2.setText(s); }
else if (status ==JFileChooser.CANCEL_OPTION)
tbImage2.setText("Please select image file!");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnColorChange;
private javax.swing.JButton btnGenerate;
private javax.swing.JEditorPane epContent;
private javax.swing.JEditorPane epFooter;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabelContentText;
private javax.swing.JLabel jLabelFooterText;
private javax.swing.JLabel jLabelImages;
private javax.swing.JLabel jLabelMenuOptions;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField tbImage1;
private javax.swing.JTextField tbImage2;
private javax.swing.JTextField tbMenu1Link;
private javax.swing.JTextField tbMenu1Title;
private javax.swing.JTextField tbMenu2Link;
private javax.swing.JTextField tbMenu2Title;
private javax.swing.JTextField tbMenu3Link;
private javax.swing.JTextField tbMenu3Title;
private javax.swing.JTextField tbMenu4Link;
private javax.swing.JTextField tbMenu4Title;
private javax.swing.JTextField tbMenu5Link;
private javax.swing.JTextField tbMenu5Title;
private javax.swing.JTextField tbMenu6Link;
private javax.swing.JTextField tbMenu6Title;
private javax.swing.JTextField tbRgbColor;
// End of variables declaration
}
Main.java:
public class Main {
public static void main(String[] args) {
MainForm mf = new MainForm();
mf.setVisible(true);
}
}
|