?? advertiseclient.java
字號:
{
updateJobButton.setEnabled(on);
addSkillButton.setEnabled(on);
removeSkillButton.setEnabled(on);
deleteJobButton.setEnabled(on);
}
private void abort (String title, String message) {
error (title, message);
shutdown ();
throw new RuntimeException(message);
}
private void error (String title, String message) {
JOptionPane.showMessageDialog(this,message,title,JOptionPane.ERROR_MESSAGE);
}
private void warning (String title, String message) {
JOptionPane.showMessageDialog(this,message,title,JOptionPane.WARNING_MESSAGE);
}
private void info (String title, String message) {
JOptionPane.showMessageDialog(this,message,title,JOptionPane.INFORMATION_MESSAGE);
}
private static class FixedField extends JTextField
{
public FixedField (int n) { super(n); }
public Dimension getMinimumSize() { return getPreferredSize(); }
public Dimension getMaximumSize() { return getPreferredSize(); }
public float getAlignmentX() { return 0;}
public float getAlignmentY() { return 0;}
}
private static final JLabel sample = new JLabel("abcdefghijklmn");
private static class FixedLabel extends JLabel
{
private Dimension size;
public FixedLabel (String s) {
super(s);
size = super.getPreferredSize();
size.width = sample.getPreferredSize().width;
}
public Dimension getPreferredSize() { return size; }
public Dimension getMinimumSize() { return size; }
public Dimension getMaximumSize() { return size; }
public float getAlignmentX() { return 0;}
public float getAlignmentY() { return 0;}
}
private boolean isNull (String s) {
return s==null || s.length()==0;
}
private boolean isNull (JTextComponent c) {
return isNull(c.getText());
}
// EJB stuff from here
private Agency getAgency(String agencyJNDI)
throws NamingException, RemoteException, CreateException, ClassCastException {
InitialContext ic = new InitialContext();
Object lookup = ic.lookup(agencyJNDI);
AgencyHome home = (AgencyHome)PortableRemoteObject.narrow(lookup, AgencyHome.class);
return home.create();
}
public String getAgencyName() {
try {
return agency.getAgencyName();
}
catch (RemoteException ex) {
error("getAgencyName",ex.toString());
ex.printStackTrace();
}
return "Agency";
}
private void loadTables(Agency agency) throws RemoteException {
location = new JComboBox(agency.getLocations().toArray());
allSkills = new JList(agency.getSkills().toArray());
allSkills.setVisibleRowCount(4);
}
private void resetDetails() throws RemoveException, RemoteException {
if (advertise != null)
{
advertise.remove();
advertise = null;
}
enableForm (false);
login.setText("");
addLogin.setText("");
addName.setText("");
addEmail.setText("");
name.setText("");
email.setText("");
address1.setText("");
address2.setText("");
jobs = new DefaultComboBoxModel();
allJobs.setModel(jobs);
resetJobDetails();
login.setEnabled(true);
}
private void resetJobDetails() throws RemoveException, RemoteException {
enableJobForm (false);
currentJob.setText("");
description.setText("");
location.setSelectedIndex(0);
skills.clear();
}
private AdvertiseHome advHome;
private void doLogin()
throws NamingException, RemoteException, RemoveException, CreateException, ClassCastException {
String name = login.getText();
if (isNull(name)) {
error ("Login","No login name specified");
return;
}
resetDetails();
login.setText(name);
if (advHome == null) {
InitialContext ic = new InitialContext();
Object lookup = ic.lookup(advertiseJNDI);
advHome = (AdvertiseHome)PortableRemoteObject.narrow(lookup, AdvertiseHome.class);
}
advertise = advHome.create(name);
loadDetails (advertise, agency);
enableForm(true);
if (!isNull(currentJob))
enableJobForm(true);
}
private void doLogoff() throws RemoveException, RemoteException {
resetDetails();
enableForm(false);
}
private void loadDetails (Advertise advertise, Agency agency) throws RemoteException {
name.setText(advertise.getName());
email.setText(advertise.getEmail());
String[] address = advertise.getAddress();
address1.setText(address[0]);
address2.setText(address[1]);
loadJobs(advertise);
}
private void loadJobs (Advertise advertise) throws RemoteException {
jobs = new DefaultComboBoxModel(advertise.getJobs());
allJobs.setModel(jobs);
}
private void doRegister()
throws NamingException, RemoveException, RemoteException, CreateException, ClassCastException {
if (isNull(addLogin) || isNull(addName) || isNull(addEmail)) {
error("Null field","You must provide a login, full name and email address");
return;
}
try {
agency.createCustomer(addLogin.getText(),addName.getText(),addEmail.getText());
String name = addLogin.getText();
resetDetails();
login.setText(name);
doLogin();
}
catch (DuplicateException ex) {
error ("Duplicate Login","Login name already in use - try another");
}
}
private void doUpdate() throws RemoteException {
if (advertise == null) {
error ("No customer", "You must login or register before updating your details");
return;
}
String[] address = new String[2];
address[0] = address1.getText();
address[1] = address2.getText();
advertise.updateDetails (name.getText(),email.getText(),address);
info ("Customer update Complete","Customer database updated successfully");
}
private void doDelete() throws RemoveException, RemoteException {
if (advertise == null) {
error ("No customer", "You must login before deleting your entry");
return;
}
try {
agency.deleteCustomer(login.getText());
resetDetails();
info ("Delete Complete","Customer entry deleted successfully");
}
catch (NotFoundException ex) {
error ("Missing Login","Internal error - customer entry not found");
}
}
private void doJobSelect() throws RemoteException, NamingException, CreateException {
if (advertise == null) {
error ("No customer", "You must login or register before selecting a job");
return;
}
if (jobs.getSize() == 0) {
error ("No Job", "You have no jobs - try creating one");
return;
}
loadJobDetails(advertise,(String)jobs.getSelectedItem(), login.getText());
enableJobForm(true);
}
private void doJobCreate() throws RemoteException, DuplicateException, NamingException, CreateException {
if (advertise == null) {
error ("No customer", "You must login or register before updating your details");
return;
}
if (isNull(newJob)) {
error ("No Job", "You must enter a job name in the new job field");
return;
}
String job = newJob.getText();
advertise.createJob(job);
loadJobs(advertise);
loadJobDetails(advertise,job,login.getText());
enableJobForm(true);
}
private AdvertiseJobHome advJobHome;
private void loadJobDetails(Advertise advertise, String ref, String customer) throws RemoteException, NamingException, CreateException {
if (advJobHome == null) {
InitialContext ic = new InitialContext();
Object lookup = ic.lookup(advertiseJobJNDI);
advJobHome = (AdvertiseJobHome)PortableRemoteObject.narrow(lookup, AdvertiseJobHome.class);
}
if (ref==null || customer==null) // check for race condition
return;
advertiseJob = advJobHome.create(ref, customer);
allJobs.setSelectedItem(ref);
newJob.setText("");
description.setText(advertiseJob.getDescription());
currentJob.setText(ref);
location.setSelectedItem(advertiseJob.getLocation());
String[] skillList = advertiseJob.getSkills();
skills.clear();
for (int i=0; i<skillList.length; i++)
skills.addElement(skillList[i]);
}
private void doJobUpdate() throws RemoteException, NotFoundException {
if (advertiseJob == null) {
error ("No Job", "You must select or create a job before updating the details");
return;
}
String[] skillList = new String[skills.size()];
skills.copyInto (skillList);
advertiseJob.updateDetails (description.getText(),(String)location.getSelectedItem(),skillList);
info ("Job update Complete","Job database updated successfully");
}
private void doJobDelete() throws RemoveException, RemoteException, NotFoundException {
if (advertiseJob == null) {
error ("No Job", "You must select a job before you can delete it");
return;
}
try {
advertise.deleteJob(currentJob.getText());
resetJobDetails();
loadJobs(advertise);
info ("Delete Complete","Customer entry deleted successfully");
}
catch (NotFoundException ex) {
error ("Missing Login","Internal error - customer entry not found");
}
}
private void doAddSkill() throws RemoteException {
if (advertiseJob == null) {
error ("No job", "You must create or select a job before modifying skills");
return;
}
Object[] skill = allSkills.getSelectedValues();
if (skill.length == 0)
warning ("No skill","You must select a skill to add from the available skills list");
for (int i=0; i<skill.length; i++) {
if (!skills.contains(skill[i]))
skills.addElement(skill[i]);
else
warning ("Duplicate skill",skill[i]+" already selected");
}
actualSkills.invalidate();
validate();
}
private void doRemoveSkill() throws RemoteException {
if (advertiseJob == null) {
error ("No job", "You must create or select a job before modifying skills");
return;
}
Object[] skill = (actualSkills.getSelectedValues());
if (skill.length == 0)
warning ("No skill","You must select a skill to remove from the skills list");
for (int i=0; i<skill.length; i++) {
if (skills.contains(skill[i]))
skills.removeElement(skill[i]);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -