Į͘ ’Œ ’‚Ż…‹Ÿ‹ ’Į̓ ĶƒĶ‚— ’‚ŌƒĮĶ
(ŻĶ‚žˆ ś‹–)


˜͟˜’› Ż‚‡œĮ ł• ’ßĶ Ķƒ ą— ’…‚Ÿ™›

encapsulation Óé–éœ ŽßĖéƒ ĮĶ ąĶĮĖĮ ڟ ŽĮ˜›Ķ‚— Łß–‰ ‘›‡ ˟œĮ߅’› ąœß˜ˆ
æĖŸŸ‚›œ ąƒ‹‚‰›


Ėé…é› ŚéŸ ąé—陃 ˟Ķߥ’›œ Å‹Ėƒ salary ŗ‡ƒĄ ĪĮ ĮĶ Łß–‰ ĶĮĖ–› ‚›Œ :Ź‹‚„
Ž‚Œœ ĮĶ Łß–‰ ŗ‡ƒĄ ޟĮ .˜ĮĖ͘’› Ī‚ƒ ĮĶ salary ŗ‡ƒĄ ą— ĖŸĶĮĖ salary()
Åé—é‡éƒĄ ’降Į ŲĖ .ŋĮ ’•Š› ŗ‡ƒĄ ŽßĶĖ Īߜ Łß–‰ įĪ‚‹ ąĖ‚Ÿ„ ‚›Į ĖĖ’›
ŽĖĮĖĶĮĶé– ß Employee ’é™éĮ ԃĮĶ ĪĮ Łß–‰ įĶ‚—…‹Ė ‘ƒĮ߅ Ų̉ salary ’—›—
Ėé…é› ĪĮ ąĖ‚é•é…é‹Į ’‚é‡ ąéƒ ŽéŸĮĶ郂éœéƒ .Åé‹Į Ž‚éŒĖßéŠ Ššé— ĶĖ ‚éžœĄ
.˟̓’› Ķ‚— ąƒ ĮĶ salary ’Į̓ Print_Yourself() Ė…› Print_your-salary()
‚éƒ ĮĶ Ėéœ›Ķ‚— Łß–‰ ˜Į߅’› Š„‹

Some_employee.print_your_salalry();

‚éŸ

some_employee.salary().print_yourself();

.˟œ— È

:˟œ—’› ąĖ‚•…‹Į ĮĶ ĶŸĪ ŃßĶ ßĖ ĪĮ ’—Ÿ Ϝ ܗ ‘›‡ ąƒ Łß–‰ ŽĖßĪ•Į ’Į̓

some_employee.add_your_salary_to( Salary the_sum )
{ the_sum.add( my_salary );
}

‚éŸ
the_sum.add( some_employee.salary() );
‚’‹—ĮĶ„ ޟƒ Ō‚ƒ…ĶĮ

æĖŸœ— ÅŸĶŸĖ› ĮĶ ŻĶ• ’ßĶ ’Ō‚ƒ…ĶĮ ’‚ÜĶ…œ— ąœß˜ˆ

ŚéŸ ĶĖ ĮĶ ĶéŸĖ‚é–é› ŽĖßéƒ Ī‚‡› checkbox ĪĮ ’ސƒ şŽß ąœß˜ˆ ąœß›œ ’Į̓
æĖœ—’› ޟŸ… ͘ŸĖ ’‹—ĮĶ„

.ÜĖ› (’ĮĪ…œĮ) ÉŌ‹ ĶĖ Ō‚ƒ…ĶĮ :Ź‹‚„

ĮĶ ‚青é‹é—ĮĶé„ ÜĖ› ÉŌ‹ ŗ‡ƒĄ Ĉ ś‹ ĶĖ ĖĖ’› Ž‚Œœ ĮĶ ÅŸŽß ßĖ ĶŸĪ Ü—Œ
Ė™Ÿ• ŽĮߜ ąƒ į͘ŸĖ ß checkbox ŽĮߜ ąƒ ’—Ÿ .ŋĮ ąĖĮĖ Ž‚Œœ ŕ ßĖ ’Į̓
ąé— Ėߌ’› ą‡ß…› ÜĖ› ÉŌ‹ Listener ˜—’› ڟ™— checkbox ’ßĶ Ķƒ ĶƒĶ‚— ’…–ß
.ĖĪ‚‹’› ܂• ĮĶ ąŌßƒĶ› Ė™Ÿ• ř‚‰ ޟĮĶĖ ß Å‹Į ąĖĶ— ͟Ÿ“… checkbox şŽß

Ųé™é…éŠé› К— ą‹ ĪĮ ŗ‡ƒĄ ą‹ ’‚’‹—ĮĶ„ ޟƒ ’ĮąĖŸˆŸ„ Ō‚ƒ…ĶĮ ŋĮĶ Å›‹ ĶĖ
Ż‚éœ ąéƒ Group ĪĮ ’Įąœß›œ employer Ż‚œ ąƒ company ĪĮ ’Įąœß›œ ĖĶĮĖ Ė߇ß
ĪĮ ’é…鋟™ company ’‹—ĮĶ„ employee Ż‚œ ąƒ person ĪĮ ’Įąœß›œ ß division
ąéŸé–ƒ ‚ƒ Ō‚ƒ…ĶĮ ąœß˜ˆ Žß˃ ĮĶ Ķ‚— ޟĮß ĖĖ’› Ž‚Œœ ĮĶ (‚ŃŠƒ) division
ąé— ĮĶ Group ’‚Å—‡ƒĄ ĪĮ hash Üߡ ڟ) qualifier ĖĮĖ ĖĮߊ Ż‚‡œĮ Ż…‹Ÿ‹
ĖĪ‚é‹éƒ ’UI ˜Į߅’› company .(ĖĖ’› Ž‚Œœ ¬ĖœĮąĖŒ ҊŒ› division name ‚ƒ
.ĖĖ Ž‚Œœ getkeys() ’œĮߊĮĶ• ‚ƒ ĮĶ division ‚žŒŠƒ ’›‚‹Į ą› ą—

¬Ėéœé—’› ƂŠ…œĮ ĮĶ ŃŠƒ Ż‚œ ĶƒĶ‚— ’…–ß ˟Ÿ‚›œ ą‡ß… ĶĮĖߛœ ’™Į߅ ąƒ ŚœŸĮ
ĪĮ Šé„é‹ .Ėéœé—’› ߇…‹‡ ĮĶ ŃŠƒ Ż‚œ hash Üߡ ĶĖ ÜĖ› ÉŌ‹ employer ŗ‡ƒĄ
Åé‹éŸ™ ޟƒ ĶĖ Division .ĖĖ Ž‚Œœ ĮĶ employees ‚… ˜—’› ŋĮߊĶĖ divison
ĮĶ Å鋟™ џ‚›Ÿ„ ޟĮ ĪĮ Š„ ¬Ėœ—’› ŋĮߊĶĖ ĮĶ ’‹—ĮĶ„ ڟ Ķ ĪĮ ‚employee
Ćéƒé‹ division ƂéŠé…éœĮ Į͟Π.ŋĮ ąˆĶ‚„—Ÿ UI ˋ͒› Ķ՜ ąƒ .ĖĖ’› Ž‚Œœ
Ō‚éƒ…ĶĮ ąœ Å‹Į ÜĖ› ÉŌ‹ Ō‚ƒ…ĶĮ ޟĮ ‚›Į .Ėߌ’› ‚employee ĪĮ ’…‹Ÿ™ ͟Ÿ“…
܃‚– ŽĮΟ› ąƒ ĮĶ įĪ‚‹ ąĖ‚Ÿ„ Bag wrapper ĪĮ ąĖ‚•…‹Į ą…ƒ™Į .‚’‹—ĮĶ„ ޟƒ
.ĖĪ‚‹’› Ž‚‹Ą ’ž‡ß…



’鋗ĮĶ„ ą—) ÜĖ› ÉŌ‹ şĖ߇ߛ ‚Ÿ .ŋĮ ąœß˜ ޟ› ąƒ ĪŸœ tab ¬shift ÅŸĶŸĖ›
Кé—ĶĖ ąéœß›œ ’Į̓ .ÜĶ…œ— ŗ‡ƒĄ ‚Ÿ ˜—’› ÜĶ…œ— ĮĶ Ķ‚…•Ķ ޟĮ (ĖĪ‚‹’› ĮĶ
ß…Ķ… ąƒ Element ’‚Å—‡ƒĄ ß Ėœ— ąĖ‚•…‹Į tab ĪĮ ˜Į߅’› form ŗ‡ƒĄ form
.˜͟˜’›œ ĶĮĶ– tab ĶĖ Invariant ’‚Ė™Ÿ• ą…ƒ™Į .˜ߌ Ķ‚Õ ŻĶ• É͌ ĶĖ

(Ō‚ƒ…ĶĮ ̓ ’œ…ƒ› ’‚’‹—ĮĶ„) cancel ’‚ą›˜Ė ß ‚ąĖĮĖ Ķ‚ƒ…Į ĪĮ͉Į

æĖߌ Ż‚‡œĮ ąœß˜ˆ cancel ’‚ą›˜Ė ÅŸĶŸĖ› ß ‚ąĖĮĖ Ķ‚ƒ…Į ĪĮ͉Į

ޟĮ ĪĮ User-Interface ’‹ŸßœĪ‚ƒ ‚ƒ ˟œĮ߅’› ‚›Œ ŋĮ ąĖŸˆŸ„ ’›— ř‚‰ ޟĮ
.˟Ÿ‚›œ ’œ‚ƒŸ…Œ„ şŽß

interface Transaction
{ public void begin();
public void commit();

public void rollback();
public int supported_rollback_levels();
}

abstract class Visual_proxy
                     extends JComponent
                     implements Transaction
{}

interface User_interface
{ public Visual_proxy proxy( String attribute_name,
boolean is_read_only );
}


.Ėéœé—’é› ’霂郟…Œ„ begin() ¬commit() łŸ™› ĪĮ Transaction interface
ĶĮĶé–Ķéƒ Ō‚éƒé…ĶĮ ŽĄ ‚ƒ ˜Į߅’› ĶƒĶ‚— Š„‹ ¬Ėœ— ’› ܂• ĮĶ ’‹—ĮĶ„ ’™ßĮ)
ÉéŌé‹ Åé—é‡éƒĄ ąéƒ commit() ’éœĮߊĮĶ• ĪĮ ܃– Ō‚ƒ…ĶĮ ޟĮ ǟ‚…œ ‚›Į ĖĪ‚‹
Ėé…é› ‚ƒ ’‹—ĮĶ„ ‚… Ėߌ’› ƃ‹ rollback() Ė…› .(ˌ ĖĮߊœ ܂‹ĶĮ ’ĮĪ…œĮ
Ī‚ƒ commit() ’œĮߊĮĶ• ĪĮ џ„ şŽß ąƒ ĮĶ ÜĖ› şŽß ß Ėœ— ĶĮĶ–Ķƒ Ō‚ƒ…ĶĮ
ąĖ‚•…‹Į łŸ™› įĪ‚‹ ܂• ͟’ ’Į̓ ݟ‹œ‚—› ޟĮ ĪĮ ˟œĮ߅’› ‚›Œ .˜ĮĖ͘
ąĖŒ ’œ‚ƒŸ…Œ„ rollback ’‚ąŸš ĖĮː… Supported_rollback_levels() .˟Ÿ‚›œ
°Ė…› ޟĮ Ėߌœ ’œ‚ƒŸ…Œ„ ’rollback ĪĮ ͘Į .˜—’› ŃĶĮΘ ÜĶ…œ— ŗ‡ƒĄ ąƒ ĮĶ
’霂郟…Œ„ ’…‹ĮߊĶĖ ’‚ąœß˜ ĪĮ ÜĶ…œ— ŗ‡ƒĄ ß…Ķ… ޟ˃ .˜ĮĖ͘’› Ī‚ƒ ĮĶ
Ķé˜Į ß ĖßéŒ’é› Ż‚é‡éœĮ commit() ł韙› Ō‹ß… ‚ąĖĮĖĶ‚ƒ…Į ĪĮ͉Į .˜—’›
Ī韜 cancel ˜— ’› ܂‹ĶĮ dialog box ٟĶŌ ĪĮ ĮĶ ’›‚“Ÿ„ ˌ‚ƒ Ī‚‡› ’ĮąĖĮĖ
ŽĖĶéŒé• Ż‚é˜œ ĶĖ Å‚Ō‚ƒ…ĶĮ roll back ‚ƒ ‚Ÿ ąĖŒ ąĖ͌• OK ą— ’œ‚›Ī ‚… ‚Ÿ
.ĖĖ͘’› įĪ‚‹ ąĖ‚Ÿ„ cancel

Menu sites

.ŋĮ Į͘ ’Œ ’‚Ż…‹Ÿ‹ ĶĖ ßœ› ÜĶ…œ— ĪĮ ’™‚†› ŕ‚‹ßĶ—Ÿ‚› OLE įĪ‚‹ ÜĖ›

ŚéŸé™é— embedded ÜßĖé‡ ŚéŸ ’ßĶ Ķéƒ ’…–ß (܋—Į ĶĖ) Å In-place įĪ‚‹ ܂•
ąé— Ė΂鋒› ĮĶ ’ĮąĶ‡œ„ ܋—Į .Ėߌ’› ąĖߌ˜ Üߡ џĮĶŸß ’Į̓ ܋—Į ¬ĖŸœ—’›
‚éƒ ąéœ Åé‹Į ܋—Į ‚ƒ Ō‚ƒ…ĶĮ ąĶ‡œ„ ޟĮ Ō‚ƒ…ĶĮ Ķ .˜—’› ’‚Œß„› ĮĶ Üߡ
.ĖĪ‚‹’› ĮĶ Ėߊ ąƒ Ķ‰œ› ’‚Scrollbar ß ‚toolbar ¬UI ܋—Į .ĖĶß

ĖéŸéœé—’› ƂŠ…œĮ ĮĶ ’›…ŸĄ ’…–ß ˜ߌ’›œ ܍…› ĖĶß ąƒ Ü‹—Į ĶĖ ßœ› ’‚Ż…ŸĄ
įĶŸß… ܋—Į .ŕ‚Ÿ ą›…‚Š џĮĶŸß Ż‚‡œĮ ą—Ÿ›‚˜œ .ĖĶß ąœ Ėߌ’› ą‡ß…› ܋—Į
.ĖĖ Ž‚Œœ ĮĶ ŽĄ ĖĶß ‚… ĖĪ‚‹’› Üߡ ĪĮ

‚é… ĖĶĮ˜ ’šŌĮ ȟ ܋—Į ĖĶ—™› ąß‰œ ĪĮ ĖĶß Å‹Į Į͘’Œ Ķ‚Ÿ‹ƒ ˜ŸĮĶ• ޟĮ
įĪ‚é‹ ąĖ‚Ÿ„ ąœß˜ˆŸ Žß˃ ą‚Ÿ‹ ąƒ‡ ڟ Üߡ ¬Ėߌ’› ŌßƒĶ› ĖĶß ąƒ ą— ’Ÿ‚‡
ŽĄ ĶĖ ‚’‹—ĮĶ„ ÜĮßĻŸß ą— Å‹Į ܂•ĶŸ’ ܉› ڟ Ϝ Form .ŋĮ UI łŸŸĪ‡ ‚Ÿ
.˜͟˜’› ĶĮĶ–

ąéƒ Ķ‚éƒ ß霛 ĶĖ ą— ’Ÿ‚’‹—ĮĶ„ ’Į̓ ’—Ķ…Œ› ܉› ’‹—ĮĶ„ ÜĮßĻŸß įĶ‚›› ĶĖ
’‚Š ŗ‡ƒĄ ‚Ÿ ŕ ą— Ėœ—’› ’‹Ķ̓ ĮĶ ’…ŸŽß ŋĮ ąĖŒ ҊŒ› ˜ĶĮĖ Ī‚Ÿœ ‚Ž•
ĶĮĶé– Ķ‚ƒ ߜ› ĶĖ ĮĶ ’Ÿßœ› ˜Į߅’› ŽĄ ĖĖ Ž‚Œœ ٟĶŌ Ėœˆ ąƒ ĮĶ ŃĖߊ ˟‚ƒ
ß霛 ĪĮ ĮĶ Ķ՜ ĖĶߛ Ż…ŸĄ ą›‚œĶƒ ŽĮĶƒĶ‚— ß ĖŒ‚ƒ ą…ŒĮĖ ’ƒ‹‚œ› ĖŸĖ ą— ĖĖ
ĪĮ ŻéŸé–…‹› ĶßŌ ąƒ Å‚Ō‚ƒ…ĶĮ ĖĪ‚‹’› ĮĶ ’Ÿßœ› ޟœˆ ’‹—ĮĶ„ .˜Ÿ‚›œ ƂŠ…œĮ
.˜ßĶ’› ’‹—ĮĶ„ Ėߊ ‚Ÿ ÜĖ› ÉŌ‹ ŗ‡ƒĄ ąƒ ßœ›

’‚ßœ› .˜Ė Ż‚‡œĮ ĮĶ Ķ‚— ą‹ ĖŸ‚ƒ ‚’‹—ĮĶ„ ߜ› Ż…‹Ÿ‹ įĶŸĢ„ ؂ԐœĮ ’Į̓
ąéŌßéƒĶé› ßéœé› ąéƒ Ķ‚ƒ ߜ›Ķ ĶĖ ĮĶ ‚Ż…ŸĄ .ĖĖ ĶĮĶ– Ķ‚ƒ ߜ› ĶĖ ĮĶ ’™—
ŗ‡ƒĄ ‚Ÿ) ’‹—ĮĶ„ .˜Ÿ‚›œ Ų̉ ’˜Ė‚‹ ąƒ ĮĶ ąĖŒ ąĖßĪ•Į Ż…ŸĄ Ķ ß ĖœŸĮĪ•Ÿƒ
ßéœé› .Ė΂鋒› ¬ĖœßŒ’› ąĖßĪ•Į ߜ› ąƒ ą— ĮĶ JMenuItem ’‚Å—‡ƒĄ (ÜĖ› ÉŌ‹
¬ĖéĖ’é› ’ßĶ ąé— ’酂閂é•é…Į ĪĮ ŋĮ Ķ‚ƒßœ› Ś™‚› ą— ’›ŸĶ• Ž‚› ‚Ÿ ş‚‹
.ĖĶĮ˜ ’šŌĮ ąœß˜ˆŸ

.Ėߌ’› ܟ˃… ’ƒŸ—Ķ… ĶƒĶ‚— ԃĮĶ ŚŸ ąƒ ÅŸ‚‹ ߜ› ԃĮĶ Ėß‡ß ‚ƒ UI

(Menu-Site) ş‚‹ ߜ› ĪĮ ąĖ‚•…‹Į

ŅéŠéŒé› ĮĶ Å韂‹ ߜ› ß ’‹—ĮĶ„ ÜĮßĻŸß ŽŸƒ ąĶ߂‰› ąß‰œ ąƒ menu-site ԃĮĶ
:˜—’› ޟŸ… ĮĶ ĶŸĪ ’‚Ė…› ԃĮĶ ŽŸĮ .˜—’›

void add_menu(Object requester, Menu menu);

ĮĶ ’鋗ĮĶ„ requester Ž‚›ß˜ĶĄ .˟ĮĪ•Į’› menu_site ’™Į Ķ‚ƒßœ› ąƒ ĮĶ ßœ›
.˜—’› ’Ÿ‚‹‚œŒ ąĖߛœ Ė‚‡ŸĮ ĮĶ Å‹ĮߊĶĖ ą—

void add_line_item(Object requester, MenuItem item, String to_this_menu);
.ĖéŸĮĪé•Į’é› ¬Åé‹Į menu_site ’é™éĮ Ķ‚éƒßœ› ĶĖ šƒ– ą— ’Ÿßœ› ąƒ Ż…ŸĄ ڟ
’é› ’Ÿ‚‹‚œŒ ¬ąĖߛœ Ė‚‡ŸĮ ĮĶ Å‹ĮߊĶĖ ą— ĮĶ ’‹—ĮĶ„ Ϝ requester Ž‚›ß˜ĶĄ
ĶĖ .Ėéœé— ’› ’Ÿ‚‹‚œŒ ąĖŒ ą•‚ŽĮ Ż…ŸĄ ą— ĮĶ ’Ÿßœ› to_this_menu ą…ŒĶ .˜—
‚éééƒ) (ŲéééŸĖĶ Üéé郂éé– ĶéŸé’) Żé…éŸĄ ßéœé› Ż‚éœ ‚éƒ ąé…éŒĶ ĮĖé…éƒĮ
Ė錜 ܍‚‰ ’–ƒ‚Ō… ͘Į .Ėߌ’› ą‹Ÿ‚–› (ąĖŒ ҊŒ› MenuComponent.setName()
MenuItem constructor ‚éŸ Menu ąéƒ Ćé™é’Į) (şßĶ Üƒ‚–) ‚Lable ‚ƒ ą…ŒĶ
‚é›éŒ ąé— ’™™›™Į ޟƒ ’‚ą›‚œĶƒ ĶĖ Ķ‚…•Ķ ޟĮ .Ėߌ’› ą‹Ÿ‚–› (Ėߌ’› ܂‹ĶĮ
ßéœé› Ż‚œ .ŋĮ ˟•› ¬ĖŸœ— ’Ÿ‚‹‚œŒ ĮĶ įψ şßĶ Üƒ‚– Lable ‚ƒ ˟Įߊ’›œ
‚éß霛 .˜—’›œ ͟Ÿ“… Ī˜Ķ ¬Å‹Į Ż…ŸĄ ߜ› ’Į̓ ĖĶ•ƒ Ķ‰œ› ą‹‚œŒ ą— Ż…ŸĄ
Ķ‚éƒßéœé› Åé‹ĮĶ Å›‹ ĶĖ Help ’ߜ› .˜ߌ’› ą•‚ŽĮ Ķ‚ƒßœ› ąƒ Å‹ĮĶ ąƒ Äˆ ĪĮ
ĖĶéŸé˜’é› ĶĮĶé–

void remove_my_menus(Object requester);

ß Menus ąéé›é
Ų̉ ¬ĖŒ‚ƒœ ŽĄ ’ßĶ Ķƒ ’Ÿßœ› ą—Ÿ…Ķߍ ĶĖ Ķ‚ƒßœ› .˜—’› Ų̉ ĮĶ ‚MenuItem
.ˌ ĖĮߊ

’›œ ’ƒ‚ŸĖĶ ĮĶ ‚ Ż…ŸĄ ß ‚ßœ› ’‹—ĮĶ„ ˜͟˜’› requester Žß˜ĶĄ Ė…› ą‹Ķ
ŲĢé‰ ĮĶ ‚靯…ŸĄ ą› ‚… ˟ߘ’› remove_my_menus() ¬Menu_site ąƒ ą—™ƒ ˜—
.˜—

Container parent = this;
while( (parent = parent.getParent()) != null )
         if( parent instanceof MenuSite )
            break;




MenuSite ą— ĮĶ ’™Į ąĶ‡œ„ ˟‚ƒ Menu_site ԃĮĶ ĶĮ ąĖ‚•…‹Į ’Į̓ ’‹—ĮĶ„
ĶéŸĪ ÅĶߍ ąƒ ’Įą–™‰ ĪĮ ˟‚ƒ Ķß՜› ޟĮ ’Į̓ .˟‚Ÿƒ ¬ąĖߛœ įĪ‚‹ ąĖ‚Ÿ„ ĮĶ
.˟œ— ąĖ‚•…‹Į

parent = SwingUtilites.getAncestorOfClass(MenuSite.class, this);



’‚é Ż…ŸĄ ߜ› ‚Ÿ ߜ› ˜Į߅’› ’‹—ĮĶ„ Ėߌ’› Ķ–…‹› Menu_site ą—œŸĮ Ӊ› ąƒ
Ż‚éŸé„ ĖéŸ‚éƒ ¬ĖßéŒ’é› Ųé–ßé…é› ’é‹é—ĮĶé„ ’é…é–ß .ĖéŸĮĪ•Ÿƒ ĮĶ Ķ՜ ĖĶߛ
ßéœé› ‚éŸ ‚éßœ› ą› ‚Ÿ ˟‚›œ ܂‹ĶĮ Menu_site ąƒ remove_my_menus(this)
ąƒ ĮĶ Ėߊ ˜Į߅’› Menu_site ÓĶ• џ„ įĪ‚‹ ąĖ‚Ÿ„) :ą…—œ .˜ߌ Ų̉ ‚Ż…ŸĄ
¬ĖœĖߛœ ą•‚ŽĮ ĮĶ ‚ž›…ŸĄ ߜ› ą— ’Ÿ‚’‹—ĮĶ„ ą› ĶĖ AncestorListener ÅĶߍ
ŲĢé‰ ĮĶ ąéŌßéƒĶ› ’‚Ż…ŸĄ ߜ› Ķ‚—Ėߊ ĶßŌ ąƒ ’‹—ĮĶ„ Ų–ß… ‚ƒ ß Ėœ— Ķ…‹Ÿ‡Ķ
Ė‚éŸĪ ‚青鋗ĮĶ„ Ķ› ÜßŌ Į͟Π¬ŻĖĶ—œ ąĖ‚•…‹Į ’Ļ…ĮĶ…‹Į ޟĮ ĪĮ Ž› .˟‚›œ
ąé…ŒĮĖ Ėß‡ß ÅŸßĶ Üƒ‚– ͟’ ÅĶߍ ąƒ ą›‚œĶƒ ’˜ĖœĪ ąĶßĖ ĶĖ Å‹‹Į Ž—›› ß Å‹Į
ĖĶߛ şŽß ̓ ’œ…ƒ› ’—Ÿ›‚œŸĖ ĶßŌ ąƒ ĮĶ ‚ßœ› ĖĮߊœ ’‹—ĮĶ„ ˟‚Œ .˜Œ‚ƒ
ąé— ĖĪ‚é‹éƒ ĮĶ ’é…é—é‡éƒĄ ˜Į߅’› ’‹—ĮĶ„ ą— ĖŸŸ‚›œ ą‡ß… (ĖĖ ĶŸŸ“… Ķ՜
Menu_site ’‚éĖé…é› ĪĮ ’—Ÿ ąƒ requester ÅĶߍ ąƒ Å—‡ƒĄ ąƒ ĮĶ ’reference
ĖßéŠ ’‚靯…ŸĄ ߜ› ĪĮ ’Įąß›‡› ͟Π˜Į߅’› ’‹—ĮĶ„ ß…Ķ… ޟ˃ .˜— ܂‹ĶĮ
.˜— Ų̉ ĮĶ

menu site įĪ‚‹ ąĖ‚Ÿ„

Ššé— įĪ‚é‹ ąĖ‚Ÿ„ ß (±· ‚… ³± Ԋ) Menu_site ԃĮĶ ŲŸĶ… ’߂‰ ͟ΠListing
ˌ‚ƒ’› ԃĮĶ ’™ŠĮĖ

Į߂‡ ĶĖ ąœ‚˜ ˜ˆ ņĮĶß

’é™éŠĮĖ Šš— ԃĮĶ ’Ļ…ĮĶ…‹Į ĪĮ ŋĮ Ī‚Ÿœ ĖĶߛ ąœ‚˜Ėœˆ ņĮĶß ą— ’›‚˜œ ĶĖ
’container ąéƒ ąé— ’›‚˜œ ĶĖ ¬ĖŒ‚ƒ К— ڟ Menu_site ͘Į .ݜ—’› ąĖ‚•…‹Į
.ŻßéŒ’é› ą‡Įߛ ܗŒ› ‚ƒ ¬ŻĶĮĖ Ī‚Ÿœ Menu_site ß JContainer ’œŸ ßĖ Ķ ĪĮ
ĖéŒ‚éƒ ŌéƒĮĶ ŚéŸ Menu_site Ķé˜Į .ĖéŸéĖ Ō‹ƒ ĮĶ Šš— ßĖ Ķ ˟œĮ߅’›œ ‚›Œ
:ݟ‚›œ įĪ‚‹ ąĖ‚Ÿ„ ĮĶ ąœ‚˜Ėœˆ ņĮĶß ĶŸĪ ÅĶߍ ąƒ ŻœĮ߅’›


class Main_frame extends JFrame implements Menu_site
{
     final Menu_site.Implementation support;

     Main_frame()
     { support = new Menu_site.Implementation(this);
         //...
     }

public void add_menu( Object requester, JMenu item )
     { support.add_menu( requester, item );
     }

public void
     add_line_item(Object requester,JMenuItem item, String here)
     { support.add_line_item(requester, item, here );
     }

public void remove_my_menus( Object requester )
     { support.remove_my_menus( requester );
     }
     //...
}



ąéœ‚é˜Ėéœéˆ Åé†ĮĶß ’œŸ .ŋĮ Menu_site ڟ ß Jframe ڟ Main_frame ŗ‡ƒĄ
’é™éŠĮĖ ’‚ž‹š— ÅĶߍ ąƒ ĮĶ ‚ŌƒĮĶ ÓĶ• џ„ ’‚įĪ‚‹ ąĖ‚Ÿ„ Ž› šß›› .˟ĶĮĖ
įĪ‚é‹ ąĖ‚Ÿ„ ‚ƒ К— įĪ‚‹ ąĖ‚Ÿ„ Į͟Π¬Żœ—’› įĪ‚‹ ąĖ‚Ÿ„ ԃĮĶ Ėߊ ĪĮ ‚…‹ŸĮ
.ŋĮ ąĮĶ› ԃĮĶ

ԃĮĶ ’‚Ė…› įĪ‚‹ ąĖ‚Ÿ„

reference (±²° ŌéŠ) menu_bar ‚éƒ ąé— Åé‹Į ’–Įß Ķ‚ƒßœ› ڟ ’߂‰ К— ޟĮ
ŽéŸĮ .ĖĪ‚é‹’é› ĮĶ ŽĄ add_menu() ¬Żé…ŸĄ ߜ› ޟ™ßĮ ŽĖŒ ąĖßĪ•Į ‚ƒ ß Ėߌ’›
’韂Ž• ą— ĮĶ requesters -- proxies ą— Ėœ— ’› ؟͐… ĮĶ hash Üߡ įĪ‚‹ ąĖ‚Ÿ„
ŽéŸĮ .Ėéœé— ’› ܍…› Ż ąƒ (±²± Ԋ) ˜—’› ŋĮߊĶĖ ‚Ż…ŸĄ ŽĖßĪ•Į ’Į̓ ĮĶ
.ĖĶĮĖ Ż…ŸĄ ‚Ÿ ߜ› ĶŸĪ Ķ ’Į̓ Item ’‚Å—‡ƒĄ ĪĮ ’Ÿ‚vector Üߡ

Ė霗’› reference ŋĮ ’™• ş‚‹ßœ› ’߂‰ ą— ĮĶ ’›ŸĶ• (±±ø Ԋ) Menu_frame
.ˌ ĖĮߊ ’Ė ĶĮĖ–› constructor Ž‚›ß˜ĶĄ ĪĮ ß

Ė΂鋒› Ķ‚ƒßœ› ŻßĪ™ ÅĶߍĶĖ (±²ø Ԋ) add_menu(...) Ė…› ‚ƒ Men_site ԃĮĶ
±³µ ÉéŌé‹ ĶĖ requester_menus() ’éœĮßéŠĮĶ• .˜—’› ą•‚ŽĮ ĮĶ įĖŸĖ‡ Ż…ŸĄ ß
ąé•‚éŽĮ Żé…ŸĄ ޟ™ßĮ ͘Į) ˜ĮĖ͘’› Ī‚ƒ ĮĶ ąĖŒ ąĖßĪ•Į ’‚Ż…ŸĄ ĪĮ ’vector
Ėéœé—’› ŋĮߊĶĖ Ż…ŸĄ ĪĮ add_menu() .(ŋĮ ’™‚Š vector ’…Œ˜Ī‚ƒ ĶĮĖ–› Ėߌ
.˜— ą›Ÿ›Ž Ķ‚ƒßœ› ąƒ ĮĶ Ėߊ ‚…

’é™éé• Ķ‚éƒßœ› Ė— ޟĮ ĶĖ .Ėߌ’› Ķ‚Õ Š„ ޟĮ ĪĮ (±³¹ Ԋ) Add_line_item
to_this_menu Ž‚é›ßé˜ĶĄ ‚éƒ ’‚éLabel ‚éŸ Ż‚éœ ‚éƒ ’Ÿßœ› ‚… Ėߌ’› ߇…‹‡
ĶĮĶé– Ń韂é›ĪĄ ĖĶßé› ‚élable ĪĮ џ„ ’›‚‹Į ą› .Ėߌ Į˟„ ¬ĖĶĮĖ įĶ‚˜Ī‚‹
ąé—œŸĮ Ӊ› ąƒ .˜ĶĮĖ ÅŸß™ßĮ ‚lable ̓ ‚Ż…ŸĄ ’›‚‹Į ’…Ķ‚ƒ ąƒ .˜͟˜’›
ß ‚éŻé…ŸĄ ߜ› vector ąƒ ß ą…Š‚‹ ĖŸĖ‡ Ż…ŸĄ Ėߌ’› ’Ÿ‚‹‚œŒ Ķ՜ ĖĶߛ ’ߜ›
.Ėߌ’› ąĖßĪ•Į Ķ‚ƒßœ› ąƒ Š„‹

‚éƒ ß Ė͟˜’› ĮĶ ‚Ż…ŸĄ ߜ›ĪĮ ’vector (±ø³ Ԋ) remove_my_menus(...) Ė…›
Ķ‚éƒßœ›) container ĪĮ ĮĶ Ėߊ ‚… ˜—’› ŋĮߊĶĖ Ż…ŸĄ Ķ ĪĮ vector џ‚›Ÿ„
.ĖĪ‚‹ Ē͂Р(ߜ› ͟Π‚Ÿ

Ƌ‚œ› ’‚Ė…›

Ž…› ß ĖŸœ— ݟ՜… ‚Ż…ŸĄ ’Į̓ ĮĶ ’…œß• ‚… ˜Ė’› Ž‚—›Į ‚›Œ ąƒ ‚Ė…› ޟĮ
.˟ĄĶĖ ŃŸ‚›œ ąƒ Ķ՜ ĖĶߛ Ŝߕ ‚ƒ Ż…ŸĄ ß ßœ›

‚éƒ ĖéŸéœĮßé…’é› ‚›Į .ŋĮ Sans Serif ß Bold ±° ąĪĮ˜Į ĶĖ ÓĶ• џ„ Ŝߕ
‚éƒ ąé— ’é›é…éŸĄ Ķé .ĖéŸĖ ĶŸŸ“… ĮĶ ŽĄ (²°° Ԋ) setFont(...) ’œĮߊĮĶ•
ąĖ‚é•é…‹Į lable ’Į̓ Ŝߕ ޟĮ ĪĮ Ėߌ ą…Š‚‹ (²±² Ԋ) Line_item ’œĮߊĮĶ•
ĖéœéœĮ߅’› Ϝ ¬ĖœßŒ’› ą…Š‚‹ ²²“ Ԋ menu(...) Ė…› ‚ƒ ą— ’Ÿ‚ßœ› .˜—’›
.˜Ÿ‚›œ ąĖ‚•…‹Į Ėߊ Lable ’Į̓ Ŝߕ ޟĮ ĪĮ

class Item

.Ėߌ’› Ż‚‡œĮ (²µ¶ Ԋ) Menu_site.Implementation.Item class ĶĖ Ķ‚— Ķ…ŒŸƒ
ąĖéŒ ŽšéĮ object ŽĮß霏 ąƒ ą— container ß (line_item) Ż…ŸĄßœ› Ė™Ÿ• ą‹
ąé—éœéŸĮ ąƒ ą…ƒ™Į) ŋĮ Jmenu ‚Ÿ JMenuBar ‚Ÿ Container .˜œ—’› ’Ÿ‚‹‚œŒ
ҊŒ› is_help_menu .(ˌ‚ƒ ą…•Ķ˜ ĶĮĶ– ߜ› ͟ΠĶĖ ‚Ÿ Ķ‚ƒßœ› ĶĖ ’™• Ż…ŸĄ
.͟Š ‚Ÿ ĖĖ’› Ž‚Œœ ĮĶ Help ’ߜ› Item ŗ‡ƒĄ ‚ŸĄ ˜—’›

ŽĄ ĪĮ ß ĖßéŒ’é› Ü‚é‹ĶĮ Item ąƒ (²ø± Ԋ) attach_menu_to_container() Ż‚Ÿ„
‚é‡éœéŸĮ ĶĖ ’降Į ܗŒ› .˜— ą›Ÿ›Ž container ąƒ ĮĶ Ėߊ ‚… ˜—’› ŋĮߊĶĖ
ĶéŸéŸ“… ĮĶ Help ߜ› ą— Ėœ—’› ؟͐… ĮĶ ’Ÿ‚Ė…› JMenuBar К— .ŋĮ swing
ß Ķé‹ Help ’ßéœé› ‚ƒ ÉŸĶ ĶßŌ ąƒ ĖŸ‚ƒ ą‡Ÿ…œĶĖ .˜œ—’›œ Ķ‚— ‚›Į ,˜Ė’›
ąĖßĪ•Į Ż‚˜œ ĶĖ ÉŸ‰ ĶßŌ ąƒ swing ą— Å‹Į ޟĮ įːƒ ܗŒ› .݌‚ƒ ą…ŒĮĖ Ķ‚—
Ü闌› ßĖ ‘•Ķ ’Į̓ Ž› .˜—’›œ ’œ‚‹Ķ ĪßĶ ąƒ ĮĶ Ķ‚ƒßœ› ‚ Ż…ŸĄ Ų̉ ‚Ÿ ŽĖŒ
.ŻĖߛœ ’™• Ķ‚ƒßœ› ޟȟ‚‡ ĮĶ ŽĄ ß Ż…Š‚‹ įĖŸĖ‡ Ķ‚ƒßœ› ڟ Ķß—Ģ›

Åé‹ĮĶ ąƒ Äˆ ĪĮ ĮĶ Ķ‚ƒßœ› ’Į߅‰› (²“¶ Ԋ) menu_bar_contents LinkedList
‚é… ŻĖĶé— ą•‚ŽĮ ŋŸ™ ŋĮĶ Å›‹ ąƒ ĮĶ ĖŸĖ‡ Item ’‚Å—‡ƒĄ Š„‹ .˜—’› ŋŸ™
’ßéœé› Äéˆ Å›‹ ĶĖ ĖŸĖ‡ ’‚Å—‡ƒĄ ř‚‰ ޟĮ ĶĖ .Ėß̓ ŋĮĶ Å›‹ ąƒ help ߜ›
.˜ˌ ą•‚ŽĮ help

’ߜ› ß GRcdfHGQ ĮĶ ’™• Item ¬(³±¶ Ԋ) detach_menu_from_container() Ė…›
(³²“ ŌéŠ) regenerate() Ė酛 .˜—’› Ų̉ menu_bar_contents list ß ąŌßƒĶ›
ŽéŸĪ˜Ÿ‚‡ Į͜Ą ß ąĖߛœ įĪ‚‹Ī‚ƒ menu_bar_contents list ٟĶŌ ĪĮ ĮĶ Ķ‚ƒßœ›
ß (³¶± ŌéŠ) requester_menus(...) ’‚éĖé…é› Ė— ąŸ–ƒ .˜— ’› ’™ƒ– Ķ‚ƒßœ›
Ԋ ĶĖϜ Unit_test К— .˜—’› įĪ‚‹ ąĖ‚Ÿ„ ĮĶ (³·· Ԋ) is_help_menu(...)
.ŋĮ ąĖ›Ą “°²




   1: package com.holub.ui;
   2: 
   3: import java.util.*;
   4: import java.io.*;
   5: import java.awt.*;
   6: import java.awt.event.*;
   7: import javax.swing.*;
   8: 
   9: import com.holub.tools.debug.Assert;
  10: 
  11: /** A Menu_site is a frame that holds a menu bar. Other objects in
  12:  *  the system (which do not have to be visual objects) can negotiate
  13:  *  with the Menu_site to have menu's placed on the site's menu
  14:  *  bar (or within submenus already found on the menu bar).
  15:  */
  16: 
  17: // Possible extensions:
  18: // * Stack existing menus and menu items if you add one with the same
  19: //   name. Restore old item from the stack when menu reverts to previous
  20: //   state.
  21: 
  22: public interface Menu_site
  23: {
  24:     /*****************************************************************
  25:      *  Adds a menu to the menu_site's main menu bar.
  26:      *  @param requester    The object that creates the menu. This
  27:      *                      does not have to be a visual object.
  28:      *  @param menu         The menu to be added    
  29:      */
  30: 
  31:     void add_menu(Object requester, JMenu menu );
  32: 
  33:     /*****************************************************************
  34:      *  Adds a line item to a menu already on the menu_site's main
  35:      *  menu bar.
  36:      *
  37:      *  @param requester    The object that creates the menu. This
  38:      *                      does not have to be a visual object.
  39:      *  @param item         The line-item to be added. This could
  40:      *                      be a single JMenuItem or an entire JMenu.
  41:      *  @param to_this_menu The label that identifies the menu to
  42:      *                      which the item is to be added. The
  43:      *                      to_this_menu string is first compared
  44:      *                      against all the (invisible) menu names
  45:      *                      (set with JMenuItem.setName())
  46:      *                      If no match is found, then the string
  47:      *                      is compared to the (visible) labels
  48:      *                      (usually passed into the JMenu or JMenuItem
  49:      *                      constructor). Note that all the menu names
  50:      *                      are examined before any of the labels are
  51:      *                      examined (by design).
  52:      *                      <p>
  53:      *                      The "Help" menu (identified by a label or
  54:      *                      name having the value "Help"). No special
  55:      *                      methods are required to manipulate it.
  56:      *
  57:      * @throws IllegalArgumentException (a RuntimeException) if the
  58:      *                      menu specified in <code>to_this_menu</code>
  59:      *                      can't be found.
  60:      */
  61: 
  62:     void add_line_item( Object requester, JMenuItem item,
  63:                                                 String to_this_menu);
  64: 
  65:     /*****************************************************************
  66:      *  Removes all JMenus and JMenuItem's that were added by this
  67:      *  requester. Note that the AWT JMenuBar is, itself, destroyed
  68:      *  when it has no menus on it.
  69:      */
  70: 
  71:     void remove_my_menus( Object requester );
  72: 
  73:     /*****************************************************************
  74:      *  This class provides support for implementing a Menu_site.
  75:      *  It has default versions of all menu-site methods, and also
  76:      *  maintains the menu bar itself (which is installed
  77:      *  on the frame, a reference to which is passed to the
  78:      *  Implementation(JFrame) constructor). Use it like this:
  79:      *
  80:      * <PRE>
  81:      *  class Main_frame extends JFrame implements Menu_site
  82:      *  {
  83:      *      final Menu_site.Implementation support;
  84:      *
  85:      *      Main_frame()
  86:      *      {   support = new Menu_site.Implementation(this);
  87:      *          //...
  88:      *      }
  89:      *
  90:      *      public void add_menu(Object requester, JMenu item )
  91:      *      {   support.add_menu( requester, item );
  92:      *      }
  93:      *
  94:      *      public void
  95:      *      add_line_item(Object requester,JMenuItem item,String here)
  96:      *      {   support.add_line_item(requester, item, here );
  97:      *      }
  98:      *
  99:      *      public void remove_my_menus(Object requester)
100:      *      {   support.remove_my_menus( requester );
101:      *      }
102:      * }
103:      * </PRE>
104:      */
105: 
106:     public static class Implementation
107:                                 implements Menu_site, Serializable
108:     {
109:          // The "requesters" table keeps track of who requested which
110:          // menu items. It is indexed by requester and contains a
111:          // Vector of Implementation.Item objects that identify all
112:          // items added by that requester.
113:          //
114:          // The Implementation also encapsulates the menu bar itself,
115:          // which it creates and installs on its menu_frame (passed
116:          // into the constructor) when the first item is added.
117: 
118:         private /*final*/ JFrame    menu_frame;
119: 
120:         private JMenuBar    menu_bar    = null;
121:         private Hashtable   requesters  = new Hashtable();
122: 
123:         //------------------------------------------------------------
124:         public Implementation( JFrame menu_frame )
125:         {   this.menu_frame = menu_frame;
126:         }
127:         //------------------------------------------------------------
128:         public void add_menu( Object requester, JMenu menu )
129:         {
130:             if( menu_bar == null )
131:                 menu_frame.setJMenuBar( menu_bar = new JMenuBar() );
132: 
133:             Item item = new Item(menu, menu_bar, is_help_menu(menu));
134: 
135:             requester_menus( requester ).addElement(item);
136:             item.attach_menu_to_container();
137:         }
138:         //------------------------------------------------------------
139:         public void add_line_item( Object requester,
140:                             JMenuItem line_item, String to_this_menu )
141:         {
142:             Assert.is_true(requester    != null, "null requester"   );
143:             Assert.is_true(line_item    != null, "null line_item"   );
144:             Assert.is_true(to_this_menu != null, "null to_this_menu");
145: 
146:             JMenu found     = null;
147:             int  menu_count = menu_bar.getMenuCount();
148: 
149:             // Find the menu into which the line item should be
150:             // inserted. First check for a match of the (invisible)
151:             // menu names.
152: 
153:             int i = 0;
154:             for(; i < menu_count; ++i )
155:             {   found = menu_bar.getMenu(i);
156:                 if( to_this_menu.equals( found.getName() ) )
157:                     break;
158:             }
159: 
160:             // If that didn't work, check for a match of
161:             // the (visible) menu labels.
162: 
163:             if( i >= menu_count )
164:                 for( i = 0 ; i < menu_count; ++i )
165:                 {   found = menu_bar.getMenu(i);
166:                     if( to_this_menu.equals( found.getText() ) )
167:                         break;
168:                 }
169: 
170:             // If you can't find it either place, throw an exception.
171: 
172:             if( i >= menu_count )
173:                 throw new IllegalArgumentException(
174:                             "add_line_item() can't find menu ("
175:                                             + to_this_menu + ")" );
176: 
177:             Item item = new Item( line_item, found, false );
178:             
179:             requester_menus( requester ).addElement( item );
180:             item.attach_menu_to_container();
181:         }
182:         //------------------------------------------------------------
183:         public void remove_my_menus( Object requester )
184:         {
185:             Vector menus = (Vector)( requesters.remove(requester) );
186: 
187:             if( menus != null )
188:             {   Enumeration e = menus.elements();
189:                 while( e.hasMoreElements() )
190:                     ((Item)(e.nextElement()))
191:                                         .detach_menu_from_container();
192:             }
193:         }
194: 
195:         /*************************************************************
196:          * Set the default font used by the {@link #line_item} and
197:          * {@link #menu} convenience methods. If you never call this
198:          * method, 10-point, SanSerif, bold is used.
199:          */
200:         static public void setFont( Font menu_font )
201:         {   Implementation.menu_font = menu_font;
202:         }
203: 
204:         static private Font menu_font =
205:                                 new Font("SansSerif", Font.BOLD, 10);
206: 
207:         /*************************************************************
208:          * This is a convenience method that manufactures menu items
209:          * with text labels. The font used is a bit more tractable
210:          * than the default Java look-and-feel font.
211:          */
212:         static public JMenuItem line_item(String text,
213:                                                 ActionListener action)
214:         {   JMenuItem item = new JMenuItem( text );
215:             item.setFont( menu_font );
216:             item.addActionListener( action );
217:             return item;
218:         }
219:         /*************************************************************
220:          * This is a convenience method that manufactures JMenu's
221:          * that use the same font as the line items manufactured by
222:          * {@link #line_item}.
223:          */
224:         static public JMenu menu(String text)
225:         {   JMenu item = new JMenu( text );
226:             item.setFont( menu_font );
227:             return item;
228:         }
229: 
230:         //============================================================
231:         // Private support methods and classes
232: 
233:         /*************************************************************
234:          * The menu_bar_contents vector contains references to the
235:          * various menus that comprise the menu bar. This kluge is
236:          * necessary because Swing does not yet support the notion
237:          * of a Help menu, and it won't let you insert menus anywhere
238:          * other than the far right of the menu bar, where the Help
239:          * menu should be. Consequently, when a new menu is added to
240:          * a menu bar, you need to clear out the existing menu bar
241:          * and rebuild it from scratch. The menu_bar_has_help
242:          * indicates that a help menu has been installed (It's
243:          * assumed to be at the far right of the menu bar.)
244:          */
245: 
246:         private final LinkedList menu_bar_contents = new LinkedList();
247: 
248:         /*************************************************************
249:          * An Item makes the association between a line item or
250:          * submenu and the MenuBar or Menu that contains it. You can
251:          * ask an Item to add or remove itself from its container.
252:          * All the weirdness associated with help menus is handled
253:          * here.
254:          */
255: 
256:         private final class Item implements Serializable
257:         {  
258:             private final JMenuItem line_item;
259:             private       Object    container;
260:             private final boolean   is_help_menu;
261: 
262:             public Item( JMenuItem line_item, Object container,
263:                                                 boolean is_help_menu)
264:             {  
265:                 Assert.is_true( container instanceof JMenu    ||
266:                                 container instanceof JMenuBar );
267: 
268:                 this.line_item      = line_item;
269:                 this.container      = container;
270:                 this.is_help_menu   = is_help_menu;
271:             }
272: 
273:             /********************************************************
274:              * Attach a menu item to it's container (either a menu
275:              * bar or a menu). Items are added at the end of the
276:              * <code>menu_bar_contents</code> list unless a help
277:              * menu exists, in which case items are added at
278:              * the penultimate position.
279:              */
280: 
281:             public final void attach_menu_to_container()
282:             {
283:                 if( container instanceof JMenu )
284:                     ((JMenu)container).add( line_item );
285:                 else
286:                 {
287:                     if( menu_bar_contents.size() <= 0 )
288:                     {  
289:                         menu_bar_contents.add( this );
290:                         ((JMenuBar)container).add( line_item );
291:                     }
292:                     else
293:                     {   Item last =
294:                                 (Item)( menu_bar_contents.getLast() );
295:                         if( !last.is_help_menu )
296:                         {   menu_bar_contents.add(this);
297:                             ((JMenuBar)container).add( line_item );
298:                         }
299:                         else    // remove the help menu, add the new
300:                         {       // item, then put the help menu back
301:                                 // (following the new item).
302: 
303:                             menu_bar_contents.removeLast();
304:                             menu_bar_contents.add( this );
305:                             menu_bar_contents.add( last );
306:                             container = regenerate();
307:                         }
308:                     }
309:                 }
310:             }
311: 
312:             /********************************************************
313:              * Remove the current menu item from its container
314:              * (either a menu bar or a menu).
315:              */
316:             public final void detach_menu_from_container()
317:             {  
318:                 if( container instanceof JMenu )
319:                 {   ((JMenu)container).remove( line_item );
320:                 }
321:                 else
322:                 {   ((JMenuBar)container).remove( line_item );
323:                     menu_bar_contents.remove( this );
324:                     container = regenerate();
325:                 }
326:             }
327: 
328: 
329:             /********************************************************
330:              * Replace the old menu bar with a new one that reflects
331:              * the current state of the <code>menu_bar_contents</code>
332:              * list.
333:              */
334:             private JMenuBar regenerate()
335:             {
336:                 // Create the new menu bar and populate it from
337:                 // the current-contents list.
338: 
339:                 menu_bar = new JMenuBar();
340:                 ListIterator i = menu_bar_contents.listIterator(0);
341:                 while( i.hasNext() )
342:                     menu_bar.add( ((Item)(i.next())).line_item );
343: 
344:                 // Replace the old menu bar with the new one.
345:                 // Calling setVisible causes the menu bar to be
346:                 // redrawn with a minimum amount of flicker. Without
347:                 // it, the redraw doesn't happen at all.
348: 
349:                 menu_frame.setJMenuBar( menu_bar );
350:                 menu_frame.setVisible( true );
351: 
352:                 return menu_bar;
353:             }
354:         }
355:         /************************************************************
356:          * Return a vector of menu items associated with a given
357:          * requester. A new (empty) vector is created and returned
358:          * if there are no menus associated with the requester at
359:          * present.
360:          */
361:         private Vector requester_menus( Object requester )
362:         {
363:             Assert.is_true( requester  != null, "Bad argument"  );
364:             Assert.is_true( requesters != null, "No requesters" );
365: 
366:             Vector menus = (Vector)( requesters.get(requester) );
367:             if( menus == null )
368:             {   menus = new Vector();
369:                 requesters.put( requester, menus );
370:             }
371:             return menus;
372:         }
373:         /************************************************************
374:          * Return true if the menu passed as an argument is the
375:          * "help" menu. The name "help" is not case sensitive.
376:          */
377:         private boolean is_help_menu( JMenu menu )
378:         {
379:             String s = menu.getName();
380:             if( s != null && s.toLowerCase().equals("help") )
381:                 return true;
382: 
383:             s = menu.getText();
384:             return (s != null && s.toLowerCase().equals("help"));
385:         }
386:     }
387: 
388:     /***************************************************************
389:      * This inner class tests the Menu_site.Implementation. Do not
390:      * Ship Menu_site$Test.class with the applications. Test the
391:      * code by invoking "java com.holub.tools.Menu_site\$Test".
392:      * The test code creates three menus:
393:      *  o   A Help menu that contains three line items, each added in
394:      *      a different way. Nothing happens when these are selected.
395:      *  o   A "Removal" menu that disappears when selected.
396:      *  o   A "Main" menu that initially contains a single line item
397:      *      called "Add an Item." Selecting this item adds a "Remove
398:      *      Menus" Item to the "Main" menu. Selecting that removes
399:      *      both the Main and Help (but not the "Removal") menus.
400:      */
401: 
402:     static class Test extends JFrame implements Menu_site
403:     {
404:         static Test instance;
405: 
406:         Test()
407:         {   setSize( 400, 200 );    
408:             addWindowListener
409:             (   new WindowAdapter()
410:                 {   public void windowClosing( WindowEvent e )
411:                     {   System.exit(1);
412:                     }
413:                 }
414:             );
415:             show();
416:         }
417:         //------------------------------------------------------------
418:         final Menu_site.Implementation support
419:                                 = new Menu_site.Implementation(this);
420: 
421:         public void add_menu(Object requester, JMenu item )
422:         {   support.add_menu( requester, item );
423:         }
424: 
425:         public void
426:         add_line_item(Object requester, JMenuItem item,
427:                                                 String to_this_menu)
428:         {   support.add_line_item(requester, item, to_this_menu );
429:         }
430: 
431:         public void remove_my_menus(Object requester)
432:         {   support.remove_my_menus( requester );
433:         }
434: 
435:         //------------------------------------------------------------
436:         static class Remove_listener implements ActionListener
437:         {   public void actionPerformed( ActionEvent e )
438:             {   instance.remove_my_menus( instance );
439:             }
440:         }
441: 
442:         static public void main( String[] args )
443:         {
444:             instance = new Test();
445: 
446:             JMenu main_menu = new JMenu( "Main" );
447:             instance.add_menu( instance, main_menu );
448: 
449:             JMenuItem add_an_item_menu = new JMenuItem(
450:                                             "Add Line Item to Menu" );
451:             add_an_item_menu.addActionListener
452:             (   new ActionListener()
453:                 {   public void actionPerformed( ActionEvent e )
454:                     {
455:                         JMenuItem remove_menus = new JMenuItem(
456:                                         "Remove Main and Help menus");
457: 
458:                         remove_menus.addActionListener
459:                         (  new ActionListener()
460:                            { public void actionPerformed(ActionEvent e)
461:                              {  instance.remove_my_menus(instance);
462:                              }
463:                            }
464:                         );
465: 
466:                         // Add line item via menu site
467:                         instance.add_line_item(instance,
468:                                                 remove_menus, "Main");
469:                     }
470:                 }
471:             );
472: 
473:             // Add this line-item directly to the "Main" menu (without
474:             // going through the menu site) just to make sure that
475:             // we can.
476: 
477:             main_menu.add( add_an_item_menu );
478: 
479:             // Create a help menu. Do it in the middle of things
480:             // to make sure that it ends up on the far right.
481:             // Use all three mechanisms for adding menu items directly
482:             // using the menu's "name," and using the menu's "text").
483: 
484:             JMenu help = new JMenu("Help"); // "Help" is the menu text
485:             help.setName("HelpMenu");       // "HelpMenu" is the
486:                                             // (invisible) menu name
487: 
488:             JMenuItem help_by_text= new JMenuItem("Added using text");
489:             JMenuItem help_by_name= new JMenuItem("Added using name");
490:             JMenuItem help_direct = new JMenuItem("Added directly");
491: 
492:             ActionListener help_listener =
493:                 new ActionListener()
494:                 {  public void actionPerformed(ActionEvent e)
495:                        {  System.out.println("No help available"); }
496:                 };
497: 
498:             help_by_text.addActionListener( help_listener );
499:             help_by_name.addActionListener( help_listener );
500:             help_direct.addActionListener ( help_listener );
501:                 
502:             instance.add_menu     ( instance, help );
503: 
504:             help    .add          (help_direct                       );
505:             instance.add_line_item(instance, help_by_text, "Help"    );
506:             instance.add_line_item(instance, help_by_name, "HelpMenu");
507: 
508:             // Create a second "requester" and have it add a Removal
509:             // menu with the name Removal_menu. Picking that menu
510:             // will remove only the menu for the current requester.
511: 
512:             final int x[] = new int[1];
513: 
514:             JMenu remove_menu = new JMenu("Removal");
515:             remove_menu.setName( "Removal_menu" );
516:             instance.add_menu( x, remove_menu );
517: 
518:             // Create and add a line item to the Removal_menu by name
519:             // (the earlier example used the Label).
520: 
521:             JMenuItem remove_me = new JMenuItem("Click_to_Remove_Me");
522:             remove_me.addActionListener
523:             (   new ActionListener()
524:                 {   public void actionPerformed( ActionEvent e )
525:                     {   instance.remove_my_menus(x);
526:                     }
527:                 }
528:             );
529:             instance.add_line_item( x, remove_me, "Removal_menu" );
530:         }
531:     }
532: }  
          




Copyright 2001/2002, PC WORLD Iran/International Data Group Inc./IT World / JavaWorld . All rights reserved.


’‡œ‹Ķ՜
ޟĮ ĖĶߛ ĶĖ ĮĶ Ėߊ Ķ՜
:˟Ÿß˜ƒ ‚› ąƒ Ć™Ō›

ŃĪĶĮ ‚ƒ ’™ŸŠ -
ŃĪĶĮ ‚ƒ -
ŃĪĶĮ’ƒ -

’œ šßŌ ’™ŸŠ -
Ƌ‚œ› -
ą‚…ß— -

’Š… ’™ŸŠ -
Ćߙԛ -
ŋŸœ ’Š… ’•‚— ąĪĮ˜Į ąƒ -


:Ż‚œ

:ڟœßĶ…—™Į ŋ„

: Å—ĶŒ Ż‚œ

܃– ’‚Å›‹–
(± ś‹–) Į͘ ’Œ ’‚Ż…‹Ÿ‹ ’Į̓ ĶƒĶ‚— ’‚ŌƒĮĶ
(² ś‹–) Į͘ ’Œ ’‚Ż…‹Ÿ‹ ’Į̓ ĶƒĶ‚— ’‚ŌƒĮĶ
(³ ś‹–) Į͘ ’Œ ’‚Ż…‹Ÿ‹ ’Į̓ ĶƒĶ‚— ’‚ŌƒĮĶ
(“ ś‹–) Į͘ ’Œ ’‚Ż…‹Ÿ‹ ’Į̓ ĶƒĶ‚— ’‚ŌƒĮĶ