import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.*;
import java.util.*;
public class ClockView {
JTextField worldClock[];
private ClockModel model;
JList cityList;
JPanel right;
private String [] cities;
ArrayList<Integer> hourDiffs;
ArrayList<String> cityNames;
int hourDiff;
String cityName;
AddRemoveButton addBtn;
AddRemoveButton minusBtn;
public ClockView(ClockModel model) {
JFrame clockFace = new JFrame("World Clock");
clockFace.setSize(600,600);
clockFace.setLocationRelativeTo(null);
clockFace.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = clockFace.getContentPane();
JPanel displayArea = new JPanel(new GridLayout(1,2));
JPanel left = new JPanel(new GridLayout(2,1));
JPanel topLeft = new JPanel(new FlowLayout());
AddRemoveButton addBtn = new AddRemoveButton("+",model);
topLeft.add(addBtn);
AddRemoveButton minusBtn = new AddRemoveButton("-",model);
topLeft.add(minusBtn);
left.add(topLeft);
JPanel bottomLeft = new JPanel();
setUpCities();
cityList=new JList(cities);
int selectedIndex = 3;
cityList.setSelectedIndex(selectedIndex);
cityList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
int currentIndex=cityList.getSelectedIndex();
JScrollPane scrollableList = new JScrollPane(cityList);
bottomLeft.add(scrollableList);
left.add(bottomLeft);
right = new JPanel(new FlowLayout());
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK,1);
Font font = new Font("Bradley Hand ITC", Font.BOLD, 24);
worldClock = new JTextField[1];
worldClock[0]=new JTextField(8);
worldClock[0].setFont(font);
worldClock[0].setEditable(false);
worldClock[0].setFocusable(false);
worldClock[0].setHorizontalAlignment(JTextField.CENTER);
cityName = cities[selectedIndex].substring(0,cities[selectedIndex].indexOf(","));
cityNames = new ArrayList<String>();
cityNames.add(cityName);
worldClock[0].setBorder(BorderFactory.createTitledBorder(lineBorder,cityName));
right.add(worldClock[0]);
hourDiff=Integer.parseInt(cities[selectedIndex].substring(cities[selectedIndex].indexOf(",")+1));
hourDiffs = new ArrayList<Integer>();
hourDiffs.add(hourDiff);
displayArea.add(left);
displayArea.add(right);
contentPane.add(displayArea);
this.model = model;
new ClockController(model);
update(worldClock,hourDiffs);
clockFace.setVisible(true);
}
public void update(JTextField [] worldClock, ArrayList<Integer> hourDiffs)
{
for(int i=0;i<worldClock.length;i++)
{
worldClock[i].setText(secondsAsTimeText(model.currentTime()+((hourDiffs.get(i))*60*60)));
}
}
public String secondsAsTimeText(long timeInSeconds) {
long hours, minutes, seconds ;
minutes = timeInSeconds / 60;
seconds = timeInSeconds % 60;
hours = minutes / 60;
hours = hours%24;
minutes = minutes % 60;
return String.format("%02d:%02d:%02d",hours,minutes,seconds);
}
public void setUpCities()
{
cities = new String []{"Accra,0","Addis Abada,+3","Adelaide,+11","Algiers,-1","Almaty,+6","Amman,+3","Amsterdam,+1","Anadyr,+12","Anchorage,-8","Ankara,+2","London,0","Paris,+1"};
}
}