-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversion.java
More file actions
174 lines (135 loc) · 5.52 KB
/
Conversion.java
File metadata and controls
174 lines (135 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
public class Conversion extends JFrame implements ActionListener {
//All Global Components
JComboBox format;
JTextArea textArea;
JLabel formattype,filelocation;
JTextField location;
JButton back,convertformat;
Conversion(){
setSize(1000,700);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
//Adding the TextArea with ScrollPane
Container contentPane = getContentPane();
contentPane.setBackground(Color.decode("#E7EDF9"));
JLabel heading=new JLabel("License Agreement");
heading.setBounds(280,0,2000,100);
heading.setFont(new Font(Font.MONOSPACED,Font.BOLD,40));
heading.setForeground(Color.decode("#0068C8"));
add(heading);
getContentPane().setLayout(new FlowLayout());
textArea = new JTextArea(30, 80);
textArea.setMargin(new Insets(20, 20, 20, 20));
//Adding the template file data in textArea for user to make further changes
try{
File obj = new File("License.txt");
Scanner robj = new Scanner(obj);
while (robj.hasNextLine()){
String dataInfo = robj.nextLine();
textArea.append(dataInfo+"\n");
}
robj.close();}
catch(FileNotFoundException e){
System.out.println("Error occurred.");
e.printStackTrace();
}
JScrollPane scrollableTextArea = new JScrollPane(textArea);
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setBounds(100,150,200,200);
getContentPane().add(scrollableTextArea);
filelocation=new JLabel("File Location :");
filelocation.setBounds(20,20,40,40);
add(filelocation);
location=new JTextField("Enter your destination Directory :");
location.setBounds(20,20,1000,40);
add(location);
formattype=new JLabel("File Format :");
formattype.setBorder(BorderFactory.createEmptyBorder(10,200,10,10));
add(formattype);
String[] str={"TXT","DOCX","PDF"};
format=new JComboBox(str);
format.setBorder(BorderFactory.createEmptyBorder(10,10,10,100));
add(format);
back=new JButton("Back");
back.setBorder(BorderFactory.createEmptyBorder(10,80,10,80));
back.setBackground(Color.decode("#0068C8"));
back.setForeground(Color.white);
back.addActionListener(this);
add(back);
convertformat=new JButton("Save");
convertformat.setBorder(BorderFactory.createEmptyBorder(10,80,10,80));
convertformat.setBackground(Color.decode("#0068C8"));
convertformat.setForeground(Color.white);
convertformat.addActionListener(this);
add(convertformat);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource()==back){
setVisible(false);
new Main();
}
else if(actionEvent.getSource()==convertformat){
String formatting=format.getSelectedItem().toString();
String address=location.getText();
String text=textArea.getText();
//Updating the template file with further changes in textArea
try (BufferedWriter writer = new BufferedWriter(new FileWriter("License.txt"))){
System.out.println(text);
writer.write(text);
writer.newLine();
}
catch(IOException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
try{
if (formatting=="TXT"){
FileInputStream fin=new FileInputStream("./License.txt");
FileOutputStream fout=new FileOutputStream(address+"/License.txt");
int c;
while ((c = fin.read()) != -1) {
fout.write(c);
}
JOptionPane.showMessageDialog(null,"File Succesfully created at :"+address+"/License.txt");
fin.close();
fout.close();
}
else if (formatting=="DOCX"){
if( new toDOCX().convertDOCX(new File("./License.txt"),address)){
JOptionPane.showMessageDialog(null,"File Succesfully created at :"+address+"/License.docx");
}
else{
JOptionPane.showMessageDialog(null,"Error! Directory doesn't exist.");
}
}
else if (formatting=="PDF"){
if (new toPDF().convertpdf(new File("./License.txt"),address)){
JOptionPane.showMessageDialog(null,"File Succesfully created at :"+address+"/License.pdf");
}
else{
JOptionPane.showMessageDialog(null,"Error! Directory doesn't exist.");
}
}
}
catch(IOException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
}
}
}